官术网_书友最值得收藏!

Compiling Go code

Go does not care about the name of the source file of an autonomous program as long as the package name is main and there is a main() function in it. This is because the main() function is where the program execution begins. This also means that you cannot have multiple main() functions in the files of a single project.

There exist two ways to run a Go program:

  • The first one, go run, just executes the Go code without generating any new files, only some temporary ones that are deleted afterward
  • The second way, go build, compiles the code, generates an executable file, and waits for you to run the executable file

This book is written on an Apple Mac OS Sierra system using the Homebrew (https://brew.sh/) version of Go. However, you should have no difficulties compiling and running the presented Go code on most Linux and FreeBSD systems, provided that you have a relatively recent version of Go.

So, the first way is as follows:

$ go run hw.go
Hello World!  

The aforementioned way allows Go to be used as a scripting language. The following is the second way:

$ go build hw.go
$ file hw
hw: Mach-O 64-bit executable x86_64

The generated executable file is named after the name of the Go source file, which is much better than a.out, which is the default filename of the executable files generated by the C compiler.

If there is an error in your code, such as a misspelled Go package name when calling a Go function, you will get the following kind of error message:

$ go run hw.go
# command-line-arguments
./hw.go:3: imported and not used: "fmt"
./hw.go:7: undefined: mt in mt.Println

If you accidentally misspell the main() function, you will get the following error message because the execution of an autonomous Go program begins from the main() function:

$ go run hw.go
# command-line-arguments
runtime.main_main f: relocation target main.main not defined
runtime.main_main f: undefined: "main.main"

Lastly, I want to show you an error message that will give you a good idea about a formatting rule of Go:

$ cat hw.gocat 
package main
    
import "fmt"
    
func main()
{
      fmt.Println("Hello World!")
}
$ go run hw.go
# command-line-arguments
./hw.go:6: syntax error: unexpected semicolon or newline before {
  

The previous error message shows us that Go prefers putting curly braces in a certain way, which is not the case with most programming languages such as Perl, C, and C++. This might look frustrating at first, but it saves you from one extra line of code and makes your programs more readable. Note that the preceding code uses the Allman formatting style, which Go does not accept.

The official explanation for this error is that Go requires the use of semicolons as statement terminators in many contexts, and the compiler automatically inserts the required semicolons when it thinks they are necessary, which in this case is at the end of a non-blank line. Therefore, putting the opening brace ({) on its own line will make the Go compiler to put a semicolon at the end of the previous line, which produces the error message.

If you think that the gofmt tool can save you from similar errors, you will be disappointed:

$ gofmt hw.go
hw.go:6:1: expected declaration, found '{'
  

The Go compiler has another rule, as you can see in the following output:

$ go run afile.go
# command-line-arguments
./afile.go:4: imported and not used: "net"

This means that you should not import packages without actually using them in your programs. Although this could have been a harmless warning message, your Go program will not get compiled. Bear in mind that similar warnings and error messages are a good indication that you are missing something, and you should try to correct them. You will create a higher quality of code if you treat warnings and errors the same.

主站蜘蛛池模板: 融水| 黑山县| 东丽区| 滦平县| 林甸县| 马尔康县| 石渠县| 武陟县| 光山县| 榆树市| 凤翔县| 高陵县| 浦江县| 云林县| 体育| 博野县| 威宁| 宜阳县| 隆林| 双峰县| 宝坻区| 方山县| 车致| 竹北市| 滁州市| 甘洛县| 江都市| 常州市| 无锡市| 滕州市| 高要市| 阳曲县| 泊头市| 历史| 福贡县| 建阳市| 阳朔县| 右玉县| 和林格尔县| 昌乐县| 九龙坡区|