Skip to content

Instantly share code, notes, and snippets.

@badsyntax
Last active May 2, 2019 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save badsyntax/6a254752207f3c513f761502cd790295 to your computer and use it in GitHub Desktop.
Save badsyntax/6a254752207f3c513f761502cd790295 to your computer and use it in GitHub Desktop.
Getting started with Go development on macOS

Install Go

Install homebrew: https://brew.sh/

Install Go:

brew install go

Edit your ~/.bashrc

vi ~/.bashrc

Add environment vars:

export GOPATH="${HOME}/go"
export GOROOT="$(brew --prefix golang)/libexec"
export PATH="$PATH:${GOPATH}/bin:${GOROOT}/bin"

Reload your shell settings:

source ~/.bashrc

Create paths:

test -d "${GOPATH}" || mkdir "${GOPATH}"
test -d "${GOPATH}/bin" || mkdir "${GOPATH}/bin"
test -d "${GOPATH}/src/github.com" || mkdir -p "${GOPATH}/src/github.com"

Creating a starter project

Create the src directory:

mkdir -p "${GOPATH}/src/github.com/yourusername/projectname"
cd "${GOPATH}/src/github.com/yourusername/projectname"

Create a hello world file:

vi hello.go

Add the following:

package main

import "fmt"

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

Build and execute:

go build
./hello

Debugging with VS Code

  1. Install the Go extension by Microsoft: https://marketplace.visualstudio.com/items?itemName=ms-vscode.Go
  2. Click on the Debug icon in the sidebar.
  3. Click on the cog icon in the Debug panel. Use the default values for the generated launch.json.
  4. Add a breakpoint in your hello.go file
  5. Click on the play icon to start debugging, and your breakpoint should be hit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment