Home >Backend Development >Golang >Try your hand at a small test and take you step by step to implement your first Go program

Try your hand at a small test and take you step by step to implement your first Go program

Go语言进阶学习
Go语言进阶学习forward
2023-07-20 16:39:481011browse

A little test, the first Go program

Every time you learn a new language, It started with Hello World.

OpenGoland, select the project directory to open, create a main.go file, the code is as follows:

package main //包名,main表示当前是一个可执行程序,一个目录下只能有一个main包名

import "fmt"//导入包

func main() {//main函数,程序入口函数
  fmt.Println("Hello world")
}

Perhaps you may not understand it, but don’t worry, type it first and it will run successfully. In Goland's code interface, right-click and run:

Try your hand at a small test and take you step by step to implement your first Go program


go build命令

上述,我们通过右击 run main.go 成功的运行了项目,但是go是静态编译语言,可以直接编译成xx.exe发给别人使用。cmd切换到项目目录下:

Try your hand at a small test and take you step by step to implement your first Go program

直接执行命令go build即可将当前main.go编译成可执行文件。

Try your hand at a small test and take you step by step to implement your first Go program

终端运行结果:

Try your hand at a small test and take you step by step to implement your first Go program

我们还可指定编译可执行文件的名字,需要 -o 参数,命令如下:

go build -o helloworld.exe

Try your hand at a small test and take you step by step to implement your first Go program

得到的文件如下:

Try your hand at a small test and take you step by step to implement your first Go program

The above is the detailed content of Try your hand at a small test and take you step by step to implement your first Go program. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:Go语言进阶学习. If there is any infringement, please contact admin@php.cn delete