Computing: DOS, OS/2 & Windows Programming

Using VSCode as IDE for Go development.

"Go was conceived in September 2007 by Robert Griesemer, Rob Pike, and Ken Thompson, all at Google, and was announced in November 2009. The goals of the language and its accompanying tools were to be expressive, efficient in both compilation and execution, and effective in writing reliable and robust programs." "Go is a general purpose programming language with advanced features and a clean syntax. Because of its wide availability on a variety of platforms, its robust well-documented common library, and its focus on good software engineering principles, Go is an ideal language to learn as your first programming language." "It has become popular as a replacement for untyped scripting languages because it balances expressiveness with safety: Go programs typically run faster than programs written in dynamic languages and suffer far fewer crashes due to unexpected type errors".

The paragraph above are how Go is described in the books An Introduction to Programming in Go and The Go Programming Language, both available as PDF on the Internet. You can download the Go binaries (not only for Windows, but also for macOS and Linux) from the Go Downloads webpage. This tutorial is about its installation (and its usage in VSCode; cf. further down in the text) on Windows 10; the Go release that I used is Go 1.22.3 (the 64-bit version, downloaded as Windows installer).

The installation is straight forward. By default, the application is installed into the usual "C:\Program Files" directory. The installer automatically adds the directory with the Go executables to the PATH environment variable.

Installing Go on Windows 10 - 'Welcome' window

The installation files do not include any IDE, code editor, or special shell. You can use Go in Windows Command Prompt; just run go.exe, followed by one of the numerous commands available. To view a list of these commands, run the command:
   go help

Go on Windows 10 - Displaying the Go version and commands help

In my text editor, I created a simple "Hello World" Go program, that I saved as main.go into the folder D:\Programming\Go\hello. Here is the code:

    package main
    import "fmt"
    func main() {
        fmt.Println("Hello World from GO!")
    }

To run this code, start Command Prompt, browse to the directory containing the source file, and execute the command
    go run main.go
To create an executable called hello.exe, use the command
    go build -o hello.exe main.go

The screenshot on the left shows how the Go source file is run; the screenshot on the right shows the build and how the executable created is run.

Go on Windows 10 - Running a simple 'Hello World' program in Command Prompt
Go on Windows 10 - Building and exeuting a simple 'Hello World' program in Command Prompt

Using VSCode as Go IDE.

Visual Studio Code (VSCode) is a lightweight but powerful free and cross-platform source code editor by Microsoft. Thanks to extensions, it may be used as IDE for a very large number of programming languages. If you haven't VSCode installed on your computer and need help for doing do, my tutorial Installing VSCode on Windows 10 may be helpful.

You can install the VS Code Go extension from within VSCode, or (as I did) download it from Visual Studio Marketplace and then use the VSCode feature Install from VSIX... You find this feature by choosing Extensions (fifth icon from top) in the left navigation bar, and click the three dots to open a menu with available commands.

Go on Windows 10 - VSCode: Install the VS Code Go extension

The screenshot below shows the description of the extension.

Go on Windows 10 - VSCode: Description of the VS Code Go extension

To create a new Go project, select Explorer in the left navigation bar (1st icon from top), and click the Open Folder button; then, browse to the folder that you have created (before) for this project (for my first Go example with VSCode, I used D:\Programming\VSCode\hello_go). You'll have to confirm that you trust the author of this folder. Then, in the main tab, click the New File... link. Name your file main.go.

Note: The Go environment is initialized the first time that you create a .go file. This initialization requires Internet access! In fact, there are some more components to be downloaded and installed. If, the first time that you use Go, you aren't connected to the Internet, you'll get the error message The "gopls" command is not available, and you will not be able to use Go in VSCode. If VSCode can reach the Internet, all necessary updates are automatically done, and the message "You are ready to Go :)" will be displayed. Note, that for further work with Go, an Internet connection will no more be required.

An important concept in Go programming is the tracking of your code's dependencies. We didn't do it before, when we tried Go in Command Prompt, but you always should start your project by creating a module file. This file, called go.mod, includes all information, that is necessary to keep builds reproducible and that gives you direct control over which module versions to use. In our simple "Hello world" project, the file includes only the name of your module (Go package; Go program) and the Go version that your code supports. But if you added dependencies, the go.mod file would also list all modules (with their versions) that your code depends on. To create the go.mod file, open a terminal (choose Terminal > New Terminal from the menu bar), and run the command
    go mod init hello_go
where "hello_go" is the name of the project folder.

Go on Windows 10 - VSCode: Project initialization (creating the go.mod file)

Now, you can add your source code (I took the one from our command line example) into the editor tab. If there are syntax errors, they will be listed. If all is ok, the message "No problems have been detected in the workspace" will be shown.

You can run a project by clicking the Run Code shortcut (triangle icon, among the icons at the upper right corner of the interface). You can view the program output in the Output tab of the messages window.

Go on Windows 10 - VSCode: Running some simple 'Hello World' source code

Maybe that I am missing something, but I don't think that there is a VSCode command to build a Go project (?). Anyway, you can easily do it in the terminal. Similarly as before in Command Prompt, enter the command
    go build -o hello.exe .
The executable is created in the folder, where the source is located. You can run the executable in the terminal. Note, that the terminal works "in a Linux way", i.e. the current directory is not added to the executables path, and you have to prefix the program name with "./" to run it (.exe being automatically added to the filename that you enter).

Go on Windows 10 - VSCode: Building and executing some simple 'Hello World' program

To terminate this tutorial, as Go code example, here a simple "Hello user" program, awaiting a name as command line argument.

    package main
    import (
        "fmt"
        "os"
    )
    func main() {
        if len(os.Args) == 1 {
            fmt.Println("Hello World!")
        } else {
            greeting := "Hello " + os.Args[1] + "!"
            fmt.Println(greeting)
        }
    }

And its output in Command Prompt.

Go on Windows 10 - Executing of some simple 'Hello User' program in Command Prompt

If you find this text helpful, please, support me and this website by signing my guestbook.