1.Go by Example: Hello World

Go by Example: 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


原文地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值