当前位置:网站首页>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 16:20:00 【Game programming】
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 mainimport "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
author : Program the ape 15528175269
Game programming , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .
边栏推荐
- laravel 8 使用passport 进行Auth验证及颁发token
- The flat life of older farmers from Beijing to Holland
- 真香,华为主动离职也给 N+1
- Database resource load management (Part 1)
- CLP information - financial standardization "14th five year plan" highlights data security
- leetcode785. 判断二分图(中等)
- Transfer learning
- 学生网站模板棕色蛋糕甜品网站设计——棕色蛋糕甜品店(4页) 美食甜品网页制作期末大作业成品_生鲜水果网页设计期末作业
- leetcode684. 冗余连接(中等)
- Factory calibrated gravity: working principle and installation position of carbon monoxide sensor, calibration standard description
猜你喜欢

Streaking? Baa!

Code farming essential SQL tuning (Part 2)

postgresql启动过程

leetcode684. 冗余连接(中等)

书籍《阅读的方法》读后感

Step 4 of installation in RF: an error is reported when installing the robotframework-selenium 2library

leetcode684. Redundant connection (medium)

JDBC debugging error, ask for guidance

Project workspace creation steps - Zezhong ar automated test tool

Code farming essential SQL tuning (Part 1)
随机推荐
Elk enterprise log analysis system
[learn FPGA programming from scratch -17]: quick start chapter - operation steps 2-5- VerilogHDL hardware description language symbol system and program framework (both software programmers and hardwa
什么是rs邮票纸?
Learn how to parse SQL from kernel code
Analysis of time complexity and space complexity
Interview high frequency algorithm question --- longest palindrome substring
Opengauss enterprise installation
Nat Commun|语言模型可以学习复杂的分子分布
C starts an external EXE file and passes in parameters
整了20张高清数据分析全知识地图,强烈建议收藏!
Code farming essential SQL tuning (Part 1)
项目经理如何击退被工作汇报支配的恐惧感?
What is a generic? Why use generics? How do I use generics? What about packaging?
The flat life of older farmers from Beijing to Holland
Princeton Dengjia student's personal account: must I have a doctorate? No, I can also be an applied scientist in a large factory as an undergraduate
CLP information - financial standardization "14th five year plan" highlights data security
Basic SQL statement - delete / update
Step 4 of installation in RF: an error is reported when installing the robotframework-selenium 2library
How to optimize the performance of compose? Find the answer through "underlying principles" | developers say · dtalk
postgresql启动过程