5

Golang: Debugging CLI Applications in VSCode

 1 year ago
source link: https://hackernoon.com/golang-debugging-cli-applications-in-vscode/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Golang: Debugging CLI Applications in VSCode

Search icon
see notifications
Notifications
💰 Game Devs: Write on #Unity Game Development and Analytics, Win from $3,000! 💰
06/05/2023
🎥 Lights, camera, internet! Check out the trailer for the Web 2.5 Documentary by HackerNoon. Out now!
05/23/2023
HackerNoon Raises $250k at $50M Valuation From Forward Research 👀
05/16/2023
Web3 Backups for All 🙌 All HackerNoon Stories Now Republish on Arweave! Read about it here!
05/16/2023
🤩 May the Features Be With You: Discover 8 New Additions That'll Boost Your HackerNoon Game
05/15/2023
🏆 HackerNoon Startups of the Year 2023: Voting and Nominations now open! 🏆
05/10/2023
🏆 Let's celebrate startups that (still) survive and thrive! The HackerNoon Startups of the Year 2023 Awards are now Live! 🏆
05/10/2023
⚠️ All writers can now collect emails and grow their newsletter directly on HackerNoon!
04/28/2023
Write on web3, win $1000!
04/03/2023
You can win $1000 by writing a story on #javascript-file-handling
04/03/2023
Share a #branding story to win $1000!
04/03/2023
Write a story on ip-geolocation, win $1000!
04/03/2023
Good news Hackers! Our stats graph has leveled up - see how much time daily people spend reading your stories!
02/23/2023
Annotate any hackernoon stories to win a free t-shirt!
02/13/2023
New Contest Alert: Win BIG with the #web-development and #ecommerce writing contests!
02/13/2023
🚨 WINNERS ALERT 🚨 #growth-marketing Writing Contest Announces Round 4 Results 🕺 💃
01/30/2023
#respectthefuckinggreen mug, OG Tee, and other Hacker Merch at 15% Discount until 1/31/2023
01/18/2023
ChatGPT is on 🔥! What do you think is next for A.I?
12/15/2022
New Writing Contest Launch! Win Up To 500 USD Per Month on #MobileDebugging Stories!
12/05/2022
HackerNoon is a Multi-language Platform: All Top Stories Now Available in 8 Languages
11/28/2022
Start off your week the right way! Here are some must-read top stories from this week, The Trail to the Sea, Culture, The Philosopher's Public Library and more 💚
11/28/2022
Recently laid off from a tech company? Share your story for free on HackerNoon!
11/14/2022
New Week, New Chance to Win from $18,000! Enter #EnterTheMetaverse Writing Contest Now!
11/08/2022
Stable Diffusion AI Image Generation is Now Available in the HackerNoon Text Editor!
11/03/2022
HN Shareholder Newsletter: Green Clock Strikes Noon :-)
10/26/2022
Vote now on HackerNoon weekly polls!
10/26/2022
Highlight any text on a story and HackerNoon will generate beautiful quote images for you to share!
10/26/2022
Don't miss out on the daily top trending stories on HackerNoon! Subscribe to TechBeat to see what people are currently interested about!
09/20/2022
HackerNoon now publishes sci-fi! Read some of our science fiction stories today and submit your own!
09/05/2022
$200k+ in Committed Writer Payouts for HackerNoon Writing Contests. Enter to Win Monthly Prizes!
08/29/2022
see 23 more
Golang: Debugging CLI Applications in VSCode by@tiago-melo

Golang: Debugging CLI Applications in VSCode

June 14th 2023 New Story
3min
by @tiago-melo

Tiago Melo

@tiago-melo

Senior Software Engineer

Open TLDRtldt arrow
Read on Terminal Reader
Read this story in a terminal
Print this story
Print this story
Read this story w/o Javascript
Read this story w/o Javascript

Too Long; Didn't Read

In this short article we'll see how to debug a [CLI] app written in [Golang] using Visual Studio Code. We'll also explore how to use IntelliSense to learn about possible attributes of existing attributes. We're going to create a simple app which takes command line arguments: go run cmd/main.go.
featured image - Golang: Debugging CLI Applications in VSCode
Your browser does not support theaudio element.
Read by Dr. One (en-US)
Audio Presented by

@tiago-melo

Tiago Melo

Senior Software Engineer


Receive Stories from @tiago-melo


Credibility

VSCode, also known as Visual Studio Code, is a highly versatile and feature-rich code editor that has won the hearts of developers worldwide. With its clean and intuitive interface, it provides a delightful coding experience, making it a top choice for many professionals and enthusiasts alike.

In a previous article of mine, we saw how to configure useful user snippets. Now I want to explore its debugging capabilities.

In this short article, we'll see how to debug a CLI app written in Golang.

Sample CLI

Here's our very simple app, which takes command-line arguments:

cmd/main.go

package main


import (
    "fmt"
    "os"


    "github.com/jessevdk/go-flags"
)


var opts struct {
    Name  string `short:"n" long:"name" description:"name" required:"true"`
    Age   int    `short:"a" long:"age" description:"age" required:"true"`
    Email string `short:"e" long:"email" description:"email" required:"true"`
}


func run(args []string) {
    flags.ParseArgs(&opts, args)
    fmt.Printf("opts.Name: %v\n", opts.Name)
    fmt.Printf("opts.Age: %v\n", opts.Age)
    fmt.Printf("opts.Email: %v\n", opts.Email)
}


func main() {
    run(os.Args)
}

Once again, as I did in this article about Golang, Kafka, and MongoDB real-time data processing, I'm using github.com/jessevdk/go-flags instead of the core flag package since it has several advantages.

To run it, we do:

$ go run cmd/main.go --name Tiago --age 39 --email [email protected]
Name: Tiago
Age: 39
Email: [email protected]

Nice. Of course, we're not doing anything useful in this simple app. But what if we have a complex CLI app and need to debug it?

Debugging it

In VSCode, click on "Run and Debug" and then "create a launch.json file":

Next, select "Go: Launch Package" option and hit enter:

Then, we'll replace the sample JSON with this one:
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch Package",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "cmd/main.go",
            "args": [
                "--name",
                "Tiago",
                "--age",
                "39",
                "--email",
                "[email protected]"
            ]
        }
    ]
}
  • program is the path to the file that contains the `main` function
  • args is the string array where you pass the desired command line arguments

When you save it, the "Run and Debug" view will look like this:

Now let's come back to our `cmd/main.go` file and put a break point: Similarly to other IDEs, you put a break point by double-clicking on the left side of the line number.

Now, back to "Run and Debug" view; just click on the green play icon:

Then the little debug toolbar will be displayed, enabling us to continue, step over, step into, step out, restart, and stop the debugging session. We'll see the output in "debug console.”
by Tiago Melo @tiago-melo.Senior Software Engineer
Read my stories
L O A D I N G
. . . comments & more!
Hackernoon hq - po box 2206, edwards, colorado 81632, usa

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK