go语言实现第一个程序-hello,world!

0.前言
工作中一直使用c++编写高并发服务器程序,但c++编写高并非服务器程序时多线程逻辑,锁机制导致的死锁,内存泄漏,崩溃等问题会耗费大量时间和精力。

听同事说go语言是专门做高并发编程的,不用考虑上面的一些问题,由google推出。想想google出品,必属精品,又和自己的工作方向相关,所以了解一下。


1.编译工具安装

使用源码安装或者命令安装都可以,ubuntu下使用命令安装:

$ sudo apt-get install golang

安装完成后,看看版本号:

$ go version
go version go1.6.2 linux/amd64
在shell终端输入go命令看看用法:

$ go
Go is a tool for managing Go source code.

Usage:

	go command [arguments]

The commands are:

	build       compile packages and dependencies
	clean       remove object files
	doc         show documentation for package or symbol
	env         print Go environment information
	fix         run go tool fix on packages
	fmt         run gofmt on package sources
	generate    generate Go files by processing source
	get         download and install packages and dependencies
	install     compile and install packages and dependencies
	list        list packages
	run         compile and run Go program
	test        test packages
	tool        run specified go tool
	version     print Go version
	vet         run go tool vet on packages

Use "go help [command]" for more information about a command.

Additional help topics:

	c           calling between Go and C
	buildmode   description of build modes
	filetype    file types
	gopath      GOPATH environment variable
	environment environment variables
	importpath  import path syntax
	packages    description of package lists
	testflag    description of testing flags
	testfunc    description of testing functions

Use "go help [topic]" for more information about that topic.

里面常用的就是build和run命令了。

2.编译运行命令

go语言的源码后缀名为.go,写一个main.go的hello world程序:

// main.go
package main

import "fmt"

func main() {
	fmt.Println("Hello, World!")
}

注释支持c++的注释方式,需要注意的一点就是函数的大括号必须放在后面,否则编译不通过。

编译命令:

go build -o main main.go
编译后就生成了执行程序main,在终端下./main可以直接运行了
编译运行命令:

$ go run main.go
Hello, World!

3.文档以及参考资料

go语言教程 | 菜鸟教程

go语言中文社区

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值