Some experiences using go language (selected)

Release: 2019-11-29 14:38:52
forward
2838 people have browsed it

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template