A Programmer's Guide To Go With Visual Studio Code
Written by Mike James   
Monday, 08 July 2019
Article Index
A Programmer's Guide To Go With Visual Studio Code
Variables & Structs
Types
Parameters
Go Control

Google's system programming language Go is becoming more mature and easier to use. Now is a good time to try it out with Visual Studio Code making it even easier. 

A Programmers Guide To Languages

languagecover

Contents

  1. Octave 
  2. Scratch 2 *revised
  3. Node.js *revised
  4. App Inventor 2 *revised
  5. Getting Started With .NET IL
  6. NetLogo
  7. Small Basic  *revised
  8. TypeScript *revised
  9. Google Apps Script
  10. Python
  11. Google's Go
    1. A Programmer's Guide To Go With Visual Studio Code
    2. A Programmer's Guide To Go Part 2 - Objects And Interfaces
    3. A Programmer's Guide To Go Part 3 - Goroutines And Concurrency
  12. Guide to F#
  13. Ruby: Object-Oriented Approach
               A Functional Language

There is an older version of this article which uses the LiteIDE instead of Visual Studio Code: A Programmer's Guide To Go With LiteIDE

Part 1  Language Basics

Data, Types, Functions and Control

gorunsGo is an easy-to-use but surprisingly powerful language. You could call it a modern version of C, but read on and find out what you think of it for yourself.  It has a clean and simple structure that is said to be statically typed. but it behaves like a dynamically typed language. It also compiles to native code, but is as easy to work with as an interpreted language. 

The bottom line is that it's worth a look and this article tells you all you need to know to get started. In Part 2 we'll take a closer look at how Go deals with objects and concurrency.

Setup 

All you have to do to set up Go is to download the appropriate binary and extract it to the correct directory and set the Path to point to the location of the bin directory where all of the Go tools live. 

https://golang.org/dl/

If you are using OSX or Windows then there are installers that you can use that will add the appropriate path.

The Windows MSI installer works very well and it gives you an instantly working installation - I recommend using it.

By default it installs the system in C:\Go

 

install

 

You can check that you have a working system by creating a simple program:

package main
import "fmt"
func main() {
 fmt.Printf("hello world\n")
}

You can use NotePad or any plain text editor and save the file as hello.go. To run it you need to open a console and type:

go run hello.go

There are all sorts of things that can go wrong with this procedure but all minor. The first is that if you use NotePad make sure that the file name is Hello.go and not Hello.go.txt. You will also have to either change directory to where the file is stored or enter the complete path to hello.go. Eventually however it should all work. Notice that it can take a few seconds to compile the program before you see the message.

As an alternative you can move on to install Visual Studio Code and try running a program from there. 

An IDE isn't essential for creating Go programs and you will hear programmers saying that it isn't necessary - all you need is an editor. This is true but for the casual programmer an IDE makes getting started easier and once you use Visual Studio Code you will quickly find that it makes you much more productive.

First you need to install Visual Studio Code. Just go to the web site https://code.visualstudio.com/ and use the installer. Next install the GitHub app:

https://git-scm.com/downloads

This isn't needed for the general use of Visual Studio Code but it is needed as part of the installation of the Go tools. Run Visual Studio Code and select Tools and Languages:

vscode1

In the list of tools on the left scroll down and select Go and install it:

vscode2

 

Select the Explorer and click Open Folder and create a new folder called Go and select it. Next  Select New File and create Hello.go. The go extension automatically loads the Go editor and you can now type in your program with Intellisense help:

// Hello project main.go
package main
import (
    "fmt"
)
func main() {
    fmt.Println("Hello World!")
}

You can run the program using the Debug menu command. While you are doing all this you will see warnings pop-up that there are commands that are not installed. You should click the Install button that appears in the bottom right and wait while the command is installed - it can take a few minutes. When everything is installed and you no longer see the warnings your program should run and its output will be in the Debug Console Window:

 vscode3

If this doesn't work the only possibilities are that you haven't installed the GitHub app or the Go tools. 



Last Updated ( Monday, 08 July 2019 )