* * This is the second Golang language learning Tutorial **# creating workspace After setting environment variables in the previous section, it is customary to create a go user for Golang learning. "' bash# useradd go# su-go ' * * Before writing the code, we should first establish the Go Workspace (Workspace). * * under MAC or Linux operating system-the Go workspace should be set to $HOME/go. So we're going to create the **go** directory under the **/home/go/** directory. -All Go source files should be placed in the **src** directory in workspace. All create SRC directories under the new Go directory to hold the source files. -All Go projects should set their own subdirectories in the SRC sequence. So create a directory **hello** in SRC to place the entire Hello World project. "' bash$ mkdir-p go/src/hello$ cd Go/src/hello ' and under Windows, the workspace should be set to C:\Users\YourName\go. So please place the go directory in C:\Users\YourName. # write code in the **/home/go/go/src/hello/** directory to create the **helloworld.go** file, the code is as follows ' gopackage main//each go file should be at the beginning of the package name Statement: Only the package name of the executable program should be main. Table name which package the file belongs to. Import "FMT"//The FMT package was introduced for printing text to standard output inside the main function. Func Main () {FMT. Println ("hello,world!")} The Func declaration function, main, calls the println function of the FMT package to do the standard output. The contents of {} are called function bodies. There are several ways to run go programs running go, which we'll cover in turn. 1. Use the **go run** command: "bash$ go run helloworld.go//Run program hello,world! Standard output ' ' 2. Using the **go install** command: "The Go install Hello//go tool will search for the Hello package in the workspace and the workspace $ ls/home/go/go///go inStall a new Bin directory under the workspace to save the installed program Bin src$ cd/home/go/go/bin $ lshello//bin directory to generate Hello's executable package $./hello//Directly execute Hello package hello,world! ' "# Compile code if you want to be able to compile this program and save the results for future use, you can use the **BUILD** subcommand: ' $ go build helloworld.go$ lshelloworld helloworld.go$. helloworldhello,world! ' # #go Build and go Install difference: Go build is only valid for main package, generate an executable binary in the current directory compilation, and the static library file generated by the dependency package is placed under $gopath/pkg. Go install general generated static library files are placed in the $gopath/pkg directory with a. A file name extension. If it is a main package, an executable binary file is generated under $gopath/bin. In this case, Helloworld.go is the main package, so no. A file is generated in the $gopath/pkg directory. **main is a special function. The entire program is run from the main function. The main function must be placed in the main package. will continue to learn about special functions in subsequent learning **99 click
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