Go by Example: Hello World
$ go run hello-world.go
hello world
Sometimes we’ll want to build our programs into binaries. We can do this using go build.
$ go build hello-world.go
$ ls
hello-world hello-world.go
We can then execute the built binary directly.
$ ./hello-world
hello world
Now that we can run and build basic Go programs, let’s learn more about the language.
为了去运行这个程序,放这个代码在hello-world.go并且用 go run来执行
$ go run hello-world.go
hello world
有的时候,我们想要去建立我们的程序编程二进制的,所以我们想要做这个用 go build这个命令
$ go build hello-world.go
$ ls
hello-world hello-world.go
然后,我们能直接执行这个编译的文件
./hello-world
Our first program will print the classic “hello world” message. Here’s the full source code.
package main import "fmt" func main() { fmt.Println("hello world") }To run the program, put the code in hello-world.go and use go run.
$ go run hello-world.go
hello world
Sometimes we’ll want to build our programs into binaries. We can do this using go build.
$ go build hello-world.go
$ ls
hello-world hello-world.go
We can then execute the built binary directly.
$ ./hello-world
hello world
Now that we can run and build basic Go programs, let’s learn more about the language.
译:
Go的例子:“你好,世界”
我们第一个程序将打印出这个经典的“hello world”信息,下面是我们全部的源码
//main包,凡是标注为main包的go文件都会被编译为可执行文件 package main //导入需要使用的包 import "fmt"// 支持格式化输出的包,就是 format 的简写 //主函数,程序的执行入口 func main() { fmt.Println("Hello World") }
为了去运行这个程序,放这个代码在hello-world.go并且用 go run来执行
$ go run hello-world.go
hello world
有的时候,我们想要去建立我们的程序编程二进制的,所以我们想要做这个用 go build这个命令
$ go build hello-world.go
$ ls
hello-world hello-world.go
然后,我们能直接执行这个编译的文件
./hello-world
hello-world
有疑问加站长微信联系(非本文作者)