这个Golang示例系列篇旨在给新手提供入门指导,通过直观的代码示例来学习Golang。 本篇我们的第一个程序,都是按照国际惯例,打印“Hello,World” 通过直观的代码示例来学习Golang。

下面是示例代码。

拷贝下面代码到文件hello-world.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
* @Author: 虞双齐
* @Date: 2015-09-22 08:41:54
* @Last Modified by: 虞双齐
* @Last Modified time: 2015-09-22 08:42:32
* @Blog: www.superzhu.com
*/
package main
import "fmt"
func main() {
fmt.Println("hello ,世界")
}

点击可以执行运行该DEMO,http://dwz.cn/1KP7A1

如何运行Go

在开发调式阶段我们可以直接执行 go run来运行程序,但有时候我们希望编译程序,以便发布到生产环境,此时通过go build编译程序,便可直接运行。 如下:

1
2
3
4
5
6
7
8
9
➜ hello-world git:(master) ✗ go run hello-world.go
hello ,世界
➜ hello-world git:(master) ✗ go build hello-world.go
➜ hello-world git:(master) ✗ ls
hello-world hello-world.go
➜ hello-world git:(master) ✗ ./hello-world
hello ,世界
➜ hello-world git:(master) ✗

上面就是Go学习开篇之旅!