• 2025年8月19日 星期二

2.Golang–HelloWord

5 月 6, 2023

Go的工作区:

GoPath设置为E:\Go_Perjctr(项目位置)

GORoot设置E:GObin(GO安装位置)

创建简单helloword.go如下:

package main

improt "fmt"

func main(){

fmt.Println("Hello word")

}

运行go helloword.go程序:
使用 go run 命令 – 在命令提示符旁,输入 go run helloword.go

在控制台上会看见 Hello World 的输出

使用 go install 命令 – 运行 go install hello,接着可以用 workspacepath/bin/hello 来运行该程序。

简单解释代码:

package main – 每一个 Go 文件都应该在开头进行 package name 的声明

import "fmt" – 我们引入了 fmt 包,用于在 main 函数里面打印文本到标准输出。

func main() - main 是一个特殊的函数。整个程序就是从 main 函数开始运行的。main 函数必须放置在 main 包中{ 和 } 分别表示 main 函数的开始和结束部分。

fmt.Println("Hello World") - fmt 包中的 Println 函数用于把文本写入标准输出。

Avatar photo

Golang