Golang installation and development using Goland on MAC.
Download and install Go
fromgolang.org/dl/Select an appropriate installation package. Select a PKG package that can be used under the MAC and install it in one-click foolproof mode.
The default installation path for go on MAC is /usr/local/go. After installation, you can use it to view the version.
go version
Copy the code
This indicates that the installation is correct. Then add GOROOT and GOPATH GOBIN to the environment variables by importing the following variables at the end of the file. ~/.bash_profile
Configure the Goland development environment
Then we can open up our IDE for development, and I’m using Goland here. If everything is installed correctly, preference–> Go –>GOROOT will display the go SDK that was installed correctly. PS: I ran into a pit here where I had originally installed GO, but kept telling me GOROOT IS NOT DEFINE, which means GOROOT IS NOT configured correctly. Then I tried to select the /usr/local/go directory on the right, but it seems that this folder is not directly accessible on MAC. Fuck me for a long time, didn’t understand, always thought I understood something wrong. When goland opened it again, it found that goland had discovered /usr/local/go.
Try a Go program
Grab a piece of code and try developing a Web application with Go. Write the following code in a random file in your local Mac directory and go build xx.go. Generate executable binaries. When I uploaded the generated executable to the server, an error occurred during execution.
Isn’t that cheating? I looked it up and said it was caused by the different compilation environment. This is what you do when you compile. GOARCH will work depending on whether your server environment is 64-bit or 32-bit.
env GOOS=linux GOARCH=amd64 go build main.go
Copy the code
Recompile and run again, and it’s OK.
Package main import (" FMT ""log" "net/ HTTP") func main() {// This method receives a string matching the route, And a func(ResponseWriter, *Request) type function http.handlefunc ("/", handler) log.fatal (http.listenandServe (":8000", Func handler(w http.responsewriter, r * http.request) {FMT.Fprintf(w, "url.path = %q\n", R.U RL. Path) / / r.U RL. The output Path of the url Path} author: togettoyou links: https://juejin.cn/post/6887468483655761933 source: the nuggets copyright owned by the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.Copy the code
Goland guide bag is red
Both GOROOT and GOPATH are set. Check that everything is correct. But it is the import package in the code directly red. This is where the solution was finally found. Goland’s own bug. My previous version was 2019.2. Then check for Update updated it. Open it again and it’s OK. learnku.com/go/t/47415
continue….