Install GO environment on Mac and vs Code

Source: Internet
Author: User
Tags install go
This is a creation in Article, where the information may have evolved or changed.

1. Download the SDK

Download the corresponding SDK to https://golang.org (need to flip the wall)

The Mac version is as follows:

1.1 Installation Version: Go1.8.darwin-arm64.pkg
Once the download is complete, simply double-click to open the installation
1.2 Compression version: Go1.8.darwin-arm64.tar
After the download is complete, you need to unzip it, then move it to the path you want to store, and configure the environment variables and other information.

2. Install the GO Environment

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

go version

3. Configure Environment variables

Open Terminal
3.1 cd ~ into the user's home directory,
3.2 ls -all See if there is a. bash_profile
3.3 vim .bash_profile Open and edit the file
3.4 Configure the path according to your actual situation, I configured the external hard drive

export GOPATH=/Volumes/gnhxsk/mygoexport GOBIN=$GOPATH/binexport PATH=$PATH:$GOBIN
  • Gopath: The root directory of daily development, go from version 1.1 to version 1.7 must set this variable, and not the same as Go installation directory, this directory is used to store the go source, go executable files, and the corresponding compiled package files. So, there are three subdirectories under this directory: src, bin, pkg
    SRC store source code (e.g.,. Go. C. h, etc.)
    The files generated by the PKG after compilation (for example:. a)
    Executable file generated after bin compilation (for convenience, this directory can be added to the $path variable, if there are multiple gopath, then use ${GOPATH//://bin:}/bin )
  • GOBIN: Is the bin directory under Gopath
  • PATH: Environment variable. The Gobin directory is required to be added to the path path, and the executable file can be run directly.
    3.5 Exit Vim and execute the following command to complete the configuration of the Golang environment variable.
    source ~/.bash_profile
    3.6go envViewing the effects of a configuration

    4.Hellow World

    SRC directory is the main directory of the development program, all the source code is placed under this directory.
    For example, $GOPATH/src/mymath indicates that MyMath is an application package or an executable application, depending on whether package is main or otherwise, the main word is an executable application, and the other is application packages.
    4.1 Execute the following code to create the MyMath folder under the SRC folder
    cd $GOPATH/srcmkdir mymath
    4.2 New File Sqrt.go
    package mymathfunc Sqrt(x float64) float64{   z := 0.0   for i := 0; i < 1000; i ++{      z -= (z * z - x) / (2 * x)   }   return z}
    4.3 Compiling the application
    4.3.1 into the corresponding application package directory. And then executego installInstallation
    4.3.2 execution in any directorygo install mymathInstallation
    After the installation is complete, you can go to the following directory to view the app package
    cd $GOPATH/pkg/${GOOS}_${GOARCH}lsmymath.a
    4.4 Calling the app package
    4.4.1 New App Package
    cd $GOPATH/srcmkdir mathappcd mathappvim mian.go
    Main.go Source:
    package mainimport(    "mymath"    "fmt")func main(){    fmt.Printf("Hello,world. Sqrt(2) = %v\n",mymath.Sqrt(2))}
    4.4.2 Compiling the program
    Go to the app directory and then executego build, a mathapp executable is generated below the directory
    ./mathapp
    Output the following:
    Hello,world. Sqrt(2) = 1.414213562373095
    4.4.3 Install the App
    Enter the directory to executego install, then in$GOPATH/binAdd an executable file Mathapp, enter the following command in terminal to execute
    mathapp
    You can also export the following content
    Hello,world. Sqrt(2) = 1.414213562373095

    5. Install vs Code

    5.1 to https://code.visualstudio.com download visual Studio code, directly using
    5.2 Installing the Go Plugin
    5.2.1 Click on the extensions icon on the right, search for Go plugin, choose Go to install, after installation, the system will prompt to restart visual Studio Code

    Screen shot 2017-03-24 11.47.24.png
    5.2.2 can turn on the AutoSave feature. Open method: Select Menu file, tick
    5.2.3 User Settings
    In the top menu, choose code-> Preferences, settings
    {  "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",// 你的Goroot  "go.gopath": "/Volumes/gnhxsk/mygo",// 你的Gopath}
    5.2.4 Install dependent package support
  • Auto Install: Select a. go file to drag into vs code, then vs code will prompt you to install the dependency package, select Install all to
  • Manual Installation: Open terminal, execute the following command
    go get -u -v github.com/nsf/gocodego get -u -v github.com/rogpeppe/godefgo get -u -v github.com/zmb3/gogetdocgo get -u -v github.com/golang/lint/golintgo get -u -v github.com/lukehoban/go-outlinego get -u -v sourcegraph.com/sqs/goreturnsgo get -u -v golang.org/x/tools/cmd/gorenamego get -u -v github.com/tpng/gopkgsgo get -u -v github.com/newhook/go-symbolsgo get -u -v golang.org/x/tools/cmd/gurugo get -u -v github.com/cweill/gotests/...
    5.2.5 Visual Studio code supports go language debugging
    Execute the following command:
    go get -v -u github.com/peterh/liner github.com/derekparker/delve/cmd/dlvbrew install go-delve/delve/delvego get -v -u github.com/peterh/liner github.com/derekparker/delve/cmd/dlv
    Modify the "Dlv-cert" certificate

    1. Turn on "Keychain Access"
    2. Open menu, Keychain Access, certificate Assistant, create a certificate
    3. Name: DIv identity Type: Self-signed certificate certificate type: Code signing and select "Let me override these defaults"
    4. Click "Continue", there is a deadline (days): 365, you can modify, 3650
    5. Continue until you see the "Specify a location for this certificate" keychain, select "System" and click the "Create" button
    6. Restart the Finder, then open keychain Access, select System, and you will see the created "Dlv-cert" certificate
    7. Right-click the "Dlv-cert" certificate, and select "Show Introduction-, trust-and code signing" to: Always Trust
    8. Open the terminal and go to the $GOPATH/src dlv source file directory under the previously installed directory: Github.com/derekparker/delve
    9. Enter the following command to recompile a DLV executing program with code signature

    GO15VENDOREXPERIMENT=1 CERT=dlv-cert make install

Problems that you may encounter

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

StackOverflow Solution:

I need to do this step belowbrew install go-delve/delve/delveand go to $GOPATH/src/github.com/derekparkergit clone https://github.com/derekparker/delve.gitCERT=dlv-cert make install(remove old dlv > brew install delve > codesign dlv)not just rungo get github.com/derekparker/delve/cmd/dlvand it works now.

By restarting Visual Studio Code, you can use it perfectly. Recommended Plugins: Vscode-icons

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.