go 语言基础语法:hello world 实例 | go 技术论坛-380玩彩网官网入口
package main
import "fmt"
func main() {
fmt.println("hello, world!")
}
保存到 hello.go 文件中。
1. 编译
$ go build hello.go
查看编译后的文件:
$ ls
hello hello.go
hello 为可执行文件,也是我们编译后的程序,直接运行:
$ ./hello
hello, world!
2. 编译 运行
运行以下命令行:
$ go run hello.go
输出:
$ go run hello.go
hello, world!
go run
做了编译和执行两动作,编译会生成 hello 文件,执行完成后此文件会被删除。
3. 安装 go 程序
$ go install hello.go
此操作会将可执行文件放置于 $gopath/bin
中,使用以下方法运行:
$ $gopath/bin/hello
hello, world!