Create Publication

We are looking for publications that demonstrate building dApps or smart contracts!
See the full list of Gitcoin bounties that are eligible for rewards.

Tutorial Thumbnail
Beginner · 15 minutes or less

Using VS Code with Go

Being able to debug is a great tool when learning new programming content. This tutorial will facilitate how to debug using Visual Studio (VS) Code, which can be used with all the code samples on the Algorand Developer Docs Portal. Other debuggers can also be used. If you are new to debugging, VS Code is the most popular tool. It supports many programming languages, is quick to load and has a wealth of documentation and extensions.

Requirements

The sample in this tutorial requires Go SDK Installation.

Background

Visual Studio Code is a lightweight but powerful source code editor that runs on your desktop and is available for Windows, macOS and Linux. It comes with built-in support for JavaScript, TypeScript and Node.js and has a rich ecosystem of extensions for other languages. VS Code can take on most of the tasks of an Integrated Development Environment (IDE) with the right configuration and plugin library extension. VS Code is open source.

Extensions are available in most development languages for VS Code. Specific install instructions are in Step 2 below. For general information see: Java, JavaScript, Go, Python and C#. See extensions for VS Code languages here.

For documentation, several other extensions are available including Grammarly and MarkDown (MD).

This tutorial walks through a Go example.

Steps

1. Download VS Code

Download VS Code from this site https://code.visualstudio.com/Download

2. Install Extensions

Open VS Code. Go to the Extensions view.
Filter the extensions list by typing your programming language of choice, such as Go, and you will see all related extensions. Install Microsoft’s Go Extension

Filter Extensions

Figure 2-1 Go related Marketplace Extensions

3. Debugging with VS Code

For general information on setting up debugging in VS Code see this:
https://code.visualstudio.com/docs/editor/debugging

Optionally, for Keyboard shortcuts and Keymap extensions, which match other editors, see this:
https://code.visualstudio.com/docs/getstarted/keybindings

4. Debugging Go with VS Code sample

In this step, we will show how to create a debug session for Go using VS Code. The steps are similar for all languages. Here is a sample Go file to test with. This code generates an Account and retrieves the mnemonic. Name this file GenerateAccount.go

package main

import (
    "fmt"

    "github.com/algorand/go-algorand-sdk/crypto"
    "github.com/algorand/go-algorand-sdk/mnemonic"
)

func main() {
    account := crypto.GenerateAccount()
    passphrase, err := mnemonic.FromPrivateKey(account.PrivateKey)

    if err != nil {
        fmt.Printf("Error creating transaction: %s\n", err)
    } else {
        fmt.Printf("My address: %s\n", account.Address)
        fmt.Printf("My passphrase: %s\n", passphrase)
    }
}

Press Run Debug Go (F5)

Figure 4-1 Press Run Debug Go (F5)

A) Select the Debug icon on the left

B) Set a breakpoint in the code. To set a breakpoint, simply click on the left margin, sometimes referred to as the gutter, on the line to break.

C) See the breakpoints set in the debug pane.

D) Press Run and Debug and you should hit your breakpoint. If you do not see a Run and Debug button, this means you already have a launch.json. Open it to see the settings, it is located in the .vscode folder in your code folder. Each configuration should appear in the Debug and Run drop-down menu.

E) You may be prompted for the debug configuration. If so, select Go.

F) Optionally, to customize Run and Debug, click on “Create a launch.json”. Launch.json is the setting file for the debugger.

Your breakpoint should be hit, and the program execution paused on the line of your breakpoint. It is highlighted.

Debugging

Figure 4-2 Debugging.

We are now debugging! See Figure 4-2 above.

A) Local Variables

B) Hover over a variable with the cursor. See the Data Tip with the contents

C) Navigate by selecting run (to next breakpoint), step over, step into, step out of or stop


Learn More

Introductory VS Code Videos

Keyboard Shortcuts charts for:
Windows,
MacOS, and
Linux