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:
上述,我们通过右击 run main.go
成功的运行了项目,但是go是静态编译语言,可以直接编译成xx.exe发给别人使用。cmd
切换到项目目录下:
直接执行命令go build
即可将当前main.go编译成可执行文件。
终端运行结果:
我们还可指定编译可执行文件的名字,需要 -o
参数,命令如下:
go build -o helloworld.exe
得到的文件如下:
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!