当前位置:网站首页>Go quick start of go language (I): the first go program
Go quick start of go language (I): the first go program
2022-06-11 02:58:00 【Procedure 15528175269】
install Go
Use Go Before the language , First install Go.Go by Linux、Mac、Windows And other platforms provide corresponding installation packages :Download and install - The Go Programming Language, Select the corresponding installation package according to your operating system and click download , Then install according to the guidance process .
With Mac For example , In addition to the official Go Installers , You can also use Homebrew Install in the terminal window :
brew install go After installation , adopt go version see Go To verify whether the installation was successful ( The latest version at the time of writing this tutorial is 1.15.7):

Development tool selection
After installing locally Go After the environment , You need to choose a handy development tool to write Go The language code , Currently, the popular development tools are JetBrains Home paid integrated development environment GoLand :

And Microsoft's free and open source Visual Studio Code, Use VS Code It needs to be installed Go Expand :

For novice , Recommended GoLand, The follow-up course of the Academy will also be Mac + GoLand Demonstrate as a local development environment .
first Go Program
Select the development tools , Next , You can start writing the first Go Language program , Still follow the tradition of programming languages , We from Hello World Start Go Language learning journey .
First , We turn on GoLand IDE, Choose to create a new hello project :

stay Location Enter the project path in , The last level of directory is the project name , stay GOROOT Choose the right Go edition ( General default ), I can click Create The button initializes the project , Once the initialization is complete , Will enter GoLand main interface :

If you have used JetBrains Home products , such as PhpStorm、WebStorm、PyCharm、IntelliJ IDEA, Should be very familiar with , I will not introduce this in detail here IDE The layout of .
Next , We need to create a new file in this project to write Go Code ,Go The source code is stored in .go In the document , So we are hello Create a new one in the directory hello.go file :


Then write Go The code is as follows :

namely :
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}Okay , A simple print Hello World Of Go The code is written , Let's briefly analyze the meaning of each line of code .
Code reading
and Java similar ,Go Use the package as the basic unit to manage code ( The analogy is PHP The namespace in ), Every Go The beginning of the source code file is a package Statement , Indicates that in this file Go The package to which the code belongs . Bag is Go The most basic unit of distribution in a language , It is also the embodiment of dependency in project management .
To generate Go Executable program , You must declare a named main My bag , And the package contains a file named main() The main function of , The function is Go The starting point of the executable program , This and C Language and Java Language is very much like , Subsequent compilation Go Project procedures should also include main The package file starts .Go Linguistic main() Functions cannot take arguments , You can't define a return value .
After the package declaration , It's a series of import sentence , Used to import the package on which the program depends ( It can be compared to PHP Pass through use Introduce classes from other namespaces to understand ). Because this sample program uses Println() function , So you need to import the fmt package .
There is one caveat , And Java and PHP Different , stay Go In language , Do not include packages that are not used in the source code file , otherwise Go The compiler will report compilation errors . This is the same as the following mandatory function left curly bracket { And the case rules for function names that will be mentioned later , All of them reflect Go Language is the design philosophy that solves software engineering problems at the language level .
all Go function ( Including the type member functions mentioned in object-oriented programming ) All with keywords func start ( This is related to PHP、Java、JavaScript Wait for the language to pass function Defining functions is different ). In addition to Go Function , Left curly bracket { Must be at the end of the function definition line , You can't start another row , otherwise Go The compiler will report compilation errors :
syntax error: unexpected semicolon or newline before {
If it's in GoLand IDE That's what it says in , The error prompt will be displayed directly :

in addition , And Python、JavaScript similar ,Go The program does not require a semicolon after each statement to indicate the end of the statement , This is also with PHP、Java And so on .
Last , The body of the function is simple , It's called fmt Provided by the package Println Function print 「hello,world」 This line of string ,Go The language can be passed directly through the package name + . No. refers to the functions defined in the package .
compile & Run the program
After understanding the meaning of the above code , Let's finally compile and run this Go Program , and PHP、Python Different ,Go Language is a compiled static language ( and Java、C equally ), Running Go Before the program , First compile it into a binary executable , We can go through Go Language provides go build Command to Go The program compiles , Then directly run the compiled executable file to execute Go Program code :

You can see , Code execution successful , Printed out 「Hello World!」.
Besides , We can also go through go run Command to achieve the same effect , This command combines the compile and execute instructions , The corresponding executable file will be executed immediately after compilation to display the execution results :

Okay , About the first Go The program is simply introduced here
边栏推荐
- GraphAcademy 課程講解:《Neo4j 圖數據科學基礎》
- Graphacademy course explanation: Fundamentals of neo4j graph data science
- 【斐波那契数列】
- jdbc工具類的問題
- Navicat premium 15 tool is automatically deleted by anti-virus protection software solution
- AOSP ~ logcat chatty line expired
- Three special data types, day3 and redis (geographic location, cardinality statistics and bitmap scene usage)
- AOSP ~ WIFI默认开启 + GPS默认关闭 + 蓝牙默认关闭 + 旋转屏幕关闭
- 码农的进阶之路 | 每日趣闻
- 反射三种方式
猜你喜欢
![[resolved] how to fix another update in progress WordPress upgrade error](/img/b1/7b9945c1a623c20288f0d232cbb0c4.jpg)
[resolved] how to fix another update in progress WordPress upgrade error

How to add cookie pop-up window in WordPress website (without plug-in)

码农的进阶之路 | 每日趣闻

If you understand the logic of mining and carbon neutrality, you will understand the 100 billion market of driverless mining areas
![[long time series prediction] aotoformer code detailed [3] model overall architecture analysis](/img/77/30215c363ae8a1324db1cbcaa5324b.png)
[long time series prediction] aotoformer code detailed [3] model overall architecture analysis

Wechat applet

牛客网:数组中只出现一次的两个数字

Flat data to tree and tree data flattening
![[C language classic]: inverted string](/img/f3/e6f37b852d22d395314628b73c9be3.jpg)
[C language classic]: inverted string

GraphAcademy 課程講解:《Neo4j 圖數據科學基礎》
随机推荐
Unity项目优化详解(持续补充ing)
Recent learning and update plan
CPT 102_ LEC 16
Arduino Uno接JQ8900-16p语音播报模块
Byte beating | the first batch of written examination for game R & D post (question solution)
【新晋开源项目】动态配置化任务编排框架 Gobrs-Async 加入Dromara开源社区
GraphAcademy 課程講解:《Neo4j 圖數據科學基礎》
Question bank and answers for 2022 melting welding and thermal cutting operation certificate examination
Learning Tai Chi Maker - esp8226 (II)
The Google search console webmaster tool cannot read the sitemap?
AOSP ~ Logcat Chatty 行过期
怎样确保消息的可靠性投递?
[MySQL 45 -10] Lesson 10 how MySQL selects indexes
What is ttfb
[implementation of bubble sorting]
[new open source project] dynamic configuration task scheduling framework gobrs async joins the dromara open source community
从绿联冲刺IPO,看手机配件市场沉浮录
数据库唯一索引和普通索引的区别?
同一个用户的两次请求SessionId竟然不一致-----记录问题
[interview question 17.04. missing numbers]