Go Development: Installing Go Environment and VS Code on Mac

Keywords: github vim brew SDK

1. download SDK

reach https://golang.org Download the corresponding SDK (you need to turn over the wall)

The Mac version is as follows:

1.1 Installation Version: go1.8.darwin-arm64.pkg
Once the download is complete, just double-click to open the installation.
1.2 Compressed Edition: go1.8.darwin-arm64.tar
After downloading, you need to decompress, then move to the path you want to store, and configure environment variables and other information.

2. Installing Go Environment

Select the installation version of version 1.8 and double-click the installation. When the installation is complete, open terminal and enter the following command to view the installed version:

go version

3. Configuring environment variables

Open terminal
3.1 cd~Enter the user's home directory.
3.2 ls-all to see if. bash_profile exists
3.3 vim.bash_profile opens and edits the file
3.4 Depending on my actual situation, I configure the path to the external hard disk.

export GOPATH=/Volumes/gnhxsk/mygo
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN
  • GOPATH: The daily development of the root directory, Go 1.1 to 1.7 version must set this variable, and can not be the same as the Goinstallation directory, this directory is used to store Gosource code, Gorunnable files, and the corresponding compiled package files. So, there are three subdirectories under this directory: src, bin, pkg
    src stores source code (such as:.Go.c.h.s, etc.)
    Files generated after pkg compilation (e.g.:. a)
    Executable files generated after bin compilation (for convenience, you can add this directory to the $PATH variable, if there are multiple gopaths, then use ${GOPATH//://bin:}/bin)
  • GOBIN: The bin directory under GOPATH
  • PATH: Environmental variables. The gobin directory needs to be added to the path path path to generate executable files and run directly.
    3.5 Exit vim and complete the configuration of golang environment variables by executing the following commands.
    source ~/.bash_profile
    3.6 go env to see the effect of configuration

    4.Hellow World

    The src directory is the main directory for developing programs. All the source code is placed under this directory.
    For example, $GOPATH/src/mymath denotes mymath as an application package or executable application, which is determined by whether the package is main or other, if main is an executable application, or if other, an application package.
    4.1 Execute the following code to create the mymath folder under the src folder
    cd $GOPATH/src
    mkdir mymath
    4.2 New file sqrt.go
    package mymath
    func Sqrt(x float64) float64{
       z := 0.0
       for i := 0; i < 1000; i ++{
          z -= (z * z - x) / (2 * x)
       }
       return z
    }
    4.3 Compiler Application
    4.3.1 Enter the corresponding application package directory. Then go install, install
    4.3.2 Execute go install mymath in any directory, install
    After the installation is complete, you can enter the following directory to view the application packages
    cd $GOPATH/pkg/${GOOS}_${GOARCH}
    ls
    mymath.a
    4.4 Call Application Package
    4.4.1 New Application Package
    cd $GOPATH/src
    mkdir mathapp
    cd mathapp
    vim mian.go
    main.go source code:
    package main
    import(
        "mymath"
        "fmt"
    )
    func main(){
        fmt.Printf("Hello,world. Sqrt(2) = %v\n",mymath.Sqrt(2))
    }
    4.4.2 Compiler
    Enter the application directory and then execute go build, under which a mathapp executable will be generated
    ./mathapp
    Output the following
    Hello,world. Sqrt(2) = 1.414213562373095
    4.4.3 Install the application
    Enter the directory to execute go install, then add an executable file mathapp under $GOPATH/bin, and enter the following command in terminal to execute
    mathapp
    You can also output the following
    Hello,world. Sqrt(2) = 1.414213562373095

    5. Install VS Code

    5.1 to https://code.visualstudio.com Download Visual Studio Code for direct use
    5.2 Install Go Plug-in
    5.2.1 Click on the Extensions icon on the right, search for the Go plug-in, select Go for installation, and after installation, the system will prompt you to restart Visual Studio Code.

    Screen snapshot 2017-03-24 11.47.24.png

    5.2.2 can turn on the automatic saving function. Opening Method: Select the menu File and check it
    5.2.3 User Settings
    Select Code - > Preferences - > Settings from the top menu
    {
      "files.autoSave": "off",
      "go.buildOnSave": true,
      "go.lintOnSave": true,
      "go.vetOnSave": true,
      "go.buildFlags": [],
      "go.lintFlags": [],
      "go.vetFlags": [],
      "go.coverOnSave": false,
      "go.useCodeSnippetsOnFunctionSuggest": false,
      "go.formatOnSave": true,
      "go.formatTool": "goreturns",
      "go.goroot": "/usr/local/go",// Your Goroot
      "go.gopath": "/Volumes/gnhxsk/mygo",// Your Gopath
    }
    5.2.4 Installation Dependency Package Support
  • Automatic Installation: Select A. go file and drag it into VS Code. Then VS Code will prompt you to install dependency packages and select install all.
  • Manual installation: Open terminal and execute the following commands
    go get -u -v github.com/nsf/gocode
    go get -u -v github.com/rogpeppe/godef
    go get -u -v github.com/zmb3/gogetdoc
    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
    go get -u -v github.com/cweill/gotests/...
    5.2.5 Visual Studio Code supports Go language debugging
    Execute the following commands:
    go get -v -u github.com/peterh/liner github.com/derekparker/delve/cmd/dlv
    brew install go-delve/delve/delve
    go get -v -u github.com/peterh/liner github.com/derekparker/delve/cmd/dlv
    Modify the "dlv-cert" certificate

    1.Open Key String Access
    2.open a menu->Keystring access->Certificate assistant->Create certificate
    3.Name: dIv Identity Type: Self-signed Certificate Certificate Type: Code Signature and Select "Let me Override these Default Values"
    4.Click Continue for a limited period (days): 365, you can modify it yourself, 3650
    5.Continue until you see the "Specify the Location for the Certificate" key string, select System, and click the Create button
    6.restart Finder,Open Keystring Access again, select System, and you will see the created KeyString Access. dlv-cert"certificate
    7.Right click"dlv-cert"Certificate, select "Show Profile"->trust->Code Signature was changed to: Always Trust
    8.open terminal,Installed before entering$GOPATH/srcDirectory dlv Source file directory:github.com/derekparker/delve
    9.Enter the following command to recompile a signed code dlv Execution procedure

    GO15VENDOREXPERIMENT=1 CERT=dlv-cert make install

Probable problems

The execution file "./dlv -h" always return >> killed:9

Stack overflow solution:

I need to do this step below

brew install go-delve/delve/delve
and go to $GOPATH/src/github.com/derekparker
git clone https://github.com/derekparker/delve.git
CERT=dlv-cert make install
(remove old dlv > brew install delve > codesign dlv)

not just run
go get github.com/derekparker/delve/cmd/dlv

and it works now.

Restart Visual Studio Code and you can use it perfectly. Recommended plug-ins: vscode-icons

Posted by daredevil88 on Tue, 18 Dec 2018 04:45:04 -0800