Go语言 - HelloWorld
概念
- Go语言是开源的语言,官网地址:https://golang.google.cn
Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.
- 简单、可靠、高效是其特点,Go语言支持并发,提供海量并行的支持。相比C++,Go语言中开启线程十分方便。
- 由于这些特点,Go语言是一门专门用于巨型中央服务器的语言。
安装(Ubuntu)
- 可以在其官网下载安装包,也可以使用apt命令安装。
sudo apt-get update
sudo apt-get install golang-go
- 安装完成后,在终端输入go,输出如下
linduo@lindo:~/Work$ 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.
HelloWorld
- hello.go
/* 包声明
* main包表示一个可独立执行的程序
*/
package main
// 引用包,引入fmt
import "fmt"
func main() {
// 定义变量
var str string = "Hello World"
// 语句
fmt.Println(str)
}
- 执行
go run hello.go
# 或编译为二进制
go build hello.go
./hello