Using vscode to build Golang development environment on Mac platform

This article introduces how to use visual studio code to build golang development environment and debug environment on Mac platform.

1. brew install golang

# install
brew install golang
# Check whether the installation is successful
$ go version
go version go1.12.7 darwin/amd64
$ brew info go
go: stable 1.13.3 (bottled), HEAD
Open source programming language to build simple/reliable/efficient software
https://golang.org
/usr/local/Cellar/go/1.12.7 (9,816 files, 452.7MB) *
  Poured from bottle on 2019-07-16 at 22:22:49
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/go.rb
==> Requirements
Required: macOS >= 10.10 ✔
==> Options
--HEAD
    Install HEAD version

2. Configure the Golang environment variable

GOROOT: Installation directory
GOPATH: Workspace directory (The directory cannot be associated with Go There are three subdirectories under this directory: src,bin,pkg)
Mac The environment variable of environment is set atvim ~/.bash_profile,Add at the end of the file

export GOROOT="/usr/local/Cellar/go/1.12.7/libexec"
export GOPATH="${HOME}/Documents/go"
export PATH="${PATH}:${GOPATH}:${GOPATH}/bin"

After setting, execute source ~ /. Bash? Profile to take effect.
Check go env to see if the environment variable is set successfully

$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/xxxx/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/xxxx/Documents/work/go_work"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.12.7/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.12.7/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/4t/rrdtjpp9487_6gf8mclrslpm0000gn/T/go-build714070253=/tmp/go-build -gno-record-gcc-switches -fno-common"

3. Install vscode

# Installation:
brew cask install visual-studio-code
# To view vscode information:
-> % brew cask info visual-studio-code
visual-studio-code: 1.3.1,e6b4afa53e9c0f54edef1673de9001e9f0f547ae
Microsoft Visual Studio Code, VS Code
https://code.visualstudio.com/
Not installed
https://github.com/caskroom/homebrew-cask/blob/master/Casks/visual-studio-code.rb
==> Contents
  Visual Studio Code.app (app)
  /Applications/Visual Studio Code.app/Contents/Resources/app/bin/code (binary)
  • vscode plug in installation
    go: development
    Vscode icons: color value, color file, directory
    Material Theme Kit: Editor UI theme
    visual studio code settings sync: synchronizing vscode profiles
    30 VSCode plug-ins that greatly improve development efficiency
  • Custom vscode

    {
     "files.autoSave": "afterDelay",
     "editor.fontSize": 14,    // Controls the font size in pixels.
     "terminal.external.osxExec": "iterm.app", // Customize terminal applications to run on OS X
     "go.gopath": "/Users/xxxx/", // Personal GOPATH personal development workspace
     "go.goroot": "/usr/local/Cellar/go/1.7.3/libexec", //GO installation directory
     "workbench.statusBar.visible": false
    }

4. Use vscode to automatically install the necessary plug-ins for debug ging

Solutions that may not be available for download (no internet access) due to network reasons:

  1. Enter cd $GOPATH/src/github.com/golang, if not, please create
  2. Download tools tools:

    git clone https://github.com/golang/tools.git tools
  3. Copy tools to $GOPATH/src/golang.org/x/tools

    cp -r $GOPATH/src/github.com/golang/tools $GOPATH/src/golang.org/x/
  4. Install the golang debug plug-in, delve
    Use brew install go delve / delve / delve or install manually
    But in practice, there will be errors. We can't find the installation package. I don't know how to solve it. We need to install it manually instead.

    Updating Homebrew...
    Error: No available formula with the name "go-delve/delve/delve"
    ==> Searching for a previously deleted formula (in the last month)...
    Warning: go-delve/delve is shallow clone. To get complete history run:
     git -C "$(brew --repo go-delve/delve)" fetch --unshallow
    
    Error: No previously deleted formula found.
    ==> Searching for similarly named formulae...
    Error: No similarly named formulae found.

    Using Xcode command line tools to install

    xcode-select --install

    Create certificate

    • Open key chain access;
    • In the menu bar, select key chain access - Certificate assistant - create certificate to start creating self signed certificate;
    • The certificate name is set to dlv; the identity type is self signed root certificate; the certificate type is code signed, and finally, tick the place where I want to overwrite these default values and choose continue;
    • The validity period should be longer.
    • Then go all the way back until the location specified for the certificate appears, select the keychain system, and then select Create;
    • The certificate is created.

      Go to $GOPATH/src/github.com/derekparker, and create your own directory if not

      cd $GOPATH/src/github.com/derekparker
      git clone https://github.com/derekparker/delve.git
      cd delve
      # install
      CERT=dlv make install
      
      go get -u -v github.com/nsf/gocode
      go get -u -v github.com/rogpeppe/godef
      go get -u -v github.com/golang/lint/golint
      go get -u -v github.com/lukehoban/go-outline
      go get -u -v sourcegraph.com/sqs/goreturns
      go get -u -v golang.org/x/tools/cmd/gorename
      go get -u -v github.com/tpng/gopkgs
      go get -u -v github.com/newhook/go-symbols
      go get -u -v golang.org/x/tools/cmd/guru

Keywords: Go github brew git Mac

Added by Codewarrior123 on Tue, 29 Oct 2019 12:09:10 +0200