Home >Backend Development >Golang >Some experiences using go language (selected)

Some experiences using go language (selected)

尚
forward
2019-11-29 14:38:522867browse

Some experiences using go language (selected)

1. Poor understanding of GOPATH and GOROOT

GOROOT is set to /usr/local/go. GOPATH is set to /usr/local/go/bin. When we go get the code, the path of the downloaded package, such as github.com/Ballwang/imooc, is GOPATH/src/github.com/Ballwang/imooc. It should be noted that imooc here is a folder rather than a file.

Note: go build compiles the source code file into a binary executable file. For example, go build test.go can generate a test executable file in the imooc file directory where test.go is located. Just ./test can output the execution results. Go run runs the program directly and outputs the results without generating a binary executable file.

2. We must ensure that the source code file we compile is located in the GOPATH/src directory, otherwise type-related exceptions will be reported.

3. The package cannot be found locally

Solution: Use the go get command in GoLand to download the required package.

go get package name You can use the code management tool to update the code package and its dependencies through remote pull, and automatically complete the compilation and installation. Before using go get, you need to install git as a code management tool. Once go get, several related dependency packages can be added to GOPATH.

4. The golang.org/x type package cannot be retrieved

Solution: In fact, golang has established a mirror library on github, such as https://github.com/golang/net That is the mirror library of https://golang.org/x/net.

You can pull the package from the mirror library, then create relevant folders in sequence according to the import hierarchy, and copy the files to the relevant files.

5. pcap.h: No such file or directory

Solution: yum install libpcap-devel

6. Methods and variables in different go files under the same package They can all call each other. Executing a file go run file.go will report undefined exceptions for methods and variables. The reason is that another go file where the undefined part is located is not compiled together.

Solution: go run *.go compile together

7, package main, func main(){} marks the entry of the go program.

For more go language, please pay attention to the go language tutorial column.

The above is the detailed content of Some experiences using go language (selected). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete