Mac Vscode Configuration Golang

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

Configuring Environment variables

Open Terminal

1 CD ~ Go to the user home directory,
2 Ls-all See if there is a. bash_profile
3 vim. Bash_profile Open and edit the file
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)
The executable file generated by the bin after compilation (for convenience, this directory can be added to the $ $PATH variable, if there are multiple gopath, then use ${gopath//://bin:}/bin to add all bin directories))

Gopath allow multiple directories, when there are multiple directories, note the delimiter, multiple directories when Windows is a semicolon, Linux system is a colon, when there are multiple Gopath, the default is to put the contents of go get in the first directory.

The above $GOPATH directory Convention has three subdirectories:

GOBIN: Is the bin directory under Gopath go install binary files generated after compilation is placed in this directory
PATH: Environment variable. The Gobin directory is required to be added to the path path, and the executable file can be run directly.

Execute the following command to complete the configuration of the Golang environment variable.

SOURCE ~/.bash_profile

Go env View the effect after configuration

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.
1 Execute the following code to create the MyMath folder under the SRC folder

cd$GOPATH/srcmkdir mymath

2 New File Sqrt.go

package mymathfuncfloat64float64{     z := 0.0     for i := 0; i < 1000; i ++{        (2 * x)     }     return z}

3 Compiling the application

3.1 Enter the corresponding application package directory. Then perform go install, installation
3.2 Execute in any directory go install mymath , install
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 Calling the app package
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.2 Compiling the program

Go to the app directory and execute go build, then a mathapp executable will be generated under that directory

./mathapp

Output the following:

Hello,world. Sqrt(2) = 1.414213562373095

4.3 Install the app

Go to this directory to perform go install, then add an executable file Mathapp under $gopath/bin, enter the following command in terminal to execute

mathapp

You can also export the following content

Hello,world. Sqrt(2) = 1.414213562373095

Install vs Code

    • 1 to https://code.visualstudio.com download Visual Studio code, directly using
    • 2 Installing the Go Plugin
    • 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
    • 2.2 You can turn on the AutoSave feature. Open method: Select Menu file, tick
    • 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",//Your goroot    "Go.gopath":"/volumes/gnhxsk/mygo",//Your Gopath}

2.4 Install dependent package support
* Automatic Installation: 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 - vGitHub.Com/nsf/gocodego get- u - vGitHub.Com/rogpeppe/godefgo get- u - vGitHub.Com/zmb3/gogetdocgo get- u - vGitHub.Com/golang/lint/golintgo get- u - vGitHub.Com/lukehoban/go-outlineGo get- u - vSourcegraph.Com/sqs/goreturnsgo get- u - vGolang.Org/x/tools/cmd/gorenamego get- u - vGitHub.Com/tpng/gopkgsgo get- u - vGitHub.Com/newhook/go-symbolsGo get- u - vGolang.Org/x/tools/cmd/gurugo get- u - vGitHub.Com/cweill/gotests/...Code with color code autocomplete (using Gocode) Snippet Quick tip information (using godef) jump to Definition (using Godef) to search for references (using go-find-references) file outline (using Go-outlineRename (use Gorename) to save the build (using Go build and go test) code formatting (using Goreturns or Goimports or gofmt) to debug the code (using delve)

5.2.5 Visual Studio code supports go language debugging
Execute the following command:

-v-u github.com/peterh/liner github.com/derekparker/delve/cmd/dlvbrew install go-delve-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: Dlv-cert 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 DLV source file directory under the previously installed $GOPATH/SRC 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

Be sure to install Xcode Command line Tools to use DLV
xcode-select --install

Go language Learning Materials

1.go Guide: Official Website link: https://go-tour-zh.appspot.com/welcome/1 (need ladder), if unable to open please use domestic link: http://tour.studygolang.com/

2. "The way to go", if you feel the difficulty of reading English, on GitHub no smell Daniel has done a Chinese translation: Https://github.com/Unknwon/the-way-to-go_ZH_CN

3. The third edition of Go Learning notes, rain Marks, the ebook on GitHub open Source: Https://github.com/qyuhen/book

4. "Go Web Programming" is an ebook written by Matchmaker (Beego's author), the GitHub Chinese catalog link: https://github.com/astaxie/build-web-application-with-golang/blob/ Master/zh/preface.md

5. "Go Programming Basics" Video tutorial, this tutorial is a no-smell Daniel, github video link: https://github.com/Unknwon/go-fundamental-programming

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.