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

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.

主站蜘蛛池模板: 辽源市| 梁平县| 静海县| 贵州省| 海兴县| 疏附县| 顺昌县| 黔南| 光山县| 凌海市| 锦屏县| 蚌埠市| 卢龙县| 洪雅县| 潼关县| 濉溪县| 会同县| 梓潼县| 南岸区| 会泽县| 彰化县| 湖南省| 正蓝旗| 启东市| 东明县| 东平县| 涿鹿县| 鄯善县| 武冈市| 朝阳市| 泰安市| 兴业县| 姜堰市| 永春县| 云霄县| 云安县| 城固县| 手机| 闽侯县| 教育| 井研县|