與C語言類似,Go程序也是從main函數(shù)開始運行,但是這個main函數(shù)必須定義在main package中。(Go語言中的package,類似于C語言中的library)
package main
import "fmt"
func main() {
fmt.Println("hello world")
}
運行Hello world有兩種方法:
方法一,由編譯器運行:
$ go run hello.go
hello world
方法二,編譯出可執(zhí)行文件后,再執(zhí)行:
$ go build hello.go
$ ./hello
hello world
參考
https://golang.org/ref/spec#Program_execution。
https://gobyexample.com/hello-world
https://www.golang-book.com/books/intro/11