與C語言類似浩螺,Go程序也是從main函數(shù)開始運(yùn)行,但是這個(gè)main函數(shù)必須定義在main package中祭陷。(Go語言中的package峦椰,類似于C語言中的library)
package main
import "fmt"
func main() {
fmt.Println("hello world")
}
運(yùn)行Hello world有兩種方法:
方法一,由編譯器運(yùn)行:
$ 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