0
点赞
收藏
分享

微信扫一扫

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

乐百川 2022-11-16 阅读 122


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语言中文社区​​

举报

相关推荐

0 条评论