- Go Systems Programming
- Mihalis Tsoukalos
- 648字
- 2021-07-02 18:07:56
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.
- Python Geospatial Development(Second Edition)
- 常用工具軟件立體化教程(微課版)
- 微信小程序全棧開發技術與實戰(微課版)
- PHP+MySQL+Dreamweaver動態網站開發從入門到精通(第3版)
- Node.js開發指南
- Android群英傳
- Julia 1.0 Programming Complete Reference Guide
- Building Serverless Web Applications
- 鴻蒙OS應用編程實戰
- Java Web應用開發項目教程
- 精通Spring:Java Web開發與Spring Boot高級功能
- jQuery從入門到精通(微課精編版)
- Cinder:Begin Creative Coding
- Spark for Data Science
- Java程序設計