当前位置:网站首页>Write and run the first go language program
Write and run the first go language program
2022-06-30 07:02:00 【Duanxiaoman】
Preface
stay Go Introduction to project directory structure In this article , We have a preliminary understanding of Go Project structure , So let's start writing the first Go Language program —— Output at console “Hello World!”.
Write the first Go Language program
1 package main // Statement main package
2
3 import "fmt" // Import fmt package , Required when printing strings
4
5 func main(){
// Statement main The main function
6 fmt.Println("Hello world!") // Print Hello world!
7 }
Let's introduce the meaning of these lines of code in detail .
package( Create a package )
Go Language “ package ” As a management unit , Every Go The source file must first declare the package it belongs to , So we'll see everyone Go The beginning of the source file is a package Statement , The format is as follows :
package name
among package Is the keyword that declares the package name ,name For the name of the package .
Go Language packages and folders are one-to-one correspondence , It has the following characteristics :
- Peer files in a directory belong to the same package .
- The package name can be different from its directory name .
- main Bag is Go Entry package of language program , One Go Language programs must have and only have one main package . If a program does not main package , Then there will be an error when compiling , Unable to generate executable .
import( Import package )
After the package declaration , yes import sentence , Used to import the package that the program depends on , The imported package name uses double quotation marks "" Surround , The format is as follows :
import "name"
among import Is the keyword of the import package ,name Is the name of the imported package .
Code No. 3 Row imported fmt package , This line of code will tell Go compiler , We need to use fmt Functions or variables in the package ,fmt Bag is Go The language standard library provides us with , Used to format the contents of input and output ( Be similar to C In language stdio.h The header file ), Similar to that os package 、io Bag, etc .
There is another point to be noted , The imported package cannot contain packages not used in the code , otherwise Go The compiler will report compilation errors , for example imported and not used: “xxx”,“xxx” Represents the package name .
You can also use a import Keyword import multiple packages , In this case, you need to use parentheses ( ) Surround the name of the bag , And each package name takes one line , That is, write it as follows :
import(
"name1"
"name2"
)
main function
Code number 5 Line creates a main function , It is Go The entry function of language program , That is, the first function to run after the program starts .main Functions can only be declared in main In bag , Cannot be declared in other packages , also , One main There must also be and only one... In the package main function .
main Function is a kind of custom function , stay Go In language , All functions are keyed func At the beginning , The definition format is as follows :
func Function name ( parameter list ) ( Return value list ){
The body of the function
}
The format description is as follows :
- Function name : By letter 、 Numbers 、 Draw the line down _ form , among , The first letter of a function name cannot be a number , also , In the same package , Function name cannot be duplicate .
- parameter list : A parameter consists of parameter variables and parameter types , for example func foo( a int, b string ).
- Return value list : It can be a list of return value types , It can also be a combination of variable name and type like parameter list , When the function has a return value , Must be used in the function body return Statement returns .
- The body of the function : Code snippets that can be called repeatedly .
Be careful :Go The left brace of a language function { Must be on the same line as the function name , Otherwise, an error will be reported .
Print Hello World
Code number 6 That's ok fmt.Println(“Hello World!”) in ,Println yes fmt A function in a package , It is used to format the output data , Like strings 、 Integers 、 Decimals, etc , Be similar to C In language printf function . Here we use Println Function to print a string , That is to say ( ) It uses "" The part of the package .
Be careful ,Println The function will wrap automatically after printing ,ln yes line Abbreviation .
Order number . yes Go A kind of language operator , This means calling fmt In bag Println function .
in addition , Code fmt.Println(“Hello World!”) Ending , No need to use ; As a Terminator ,Go The compiler will automatically add , Of course , Add here ; It's OK, too .
compile & function
Go Language is a compiled static language ( and C The language is the same ), So it's running Go Before language program , First compile it into binary executable file .
Can pass Go Language provides go build perhaps go run Command to Go Language program to compile :
go build commandCan be Go The language program code is compiled into binary executable files , But we need to run the binary manually ;go run commandIt's more convenient , It will run directly after compilation Go Language program , A temporary file will be generated during compilation , But the executable file will not be generated , This feature is very suitable for debugging programs .
It's already written Go Language program , We use go run Run the command :
Come here , I believe you will know how to create a simple Go Language program , Let's try it !
边栏推荐
- [my advanced OpenGL learning journey] about the access methods of vector and matrix classification of OpenGL shaders: xyzw/rgba/stpq and array subscripts
- Essence of signal slot macros signal and slot
- [fuzzy neural network] mobile robot path planning based on Fuzzy Neural Network
- 2、 Layout system
- 15 minutes learn to use JWT
- 【SemiDrive源码分析】【X9芯片启动流程】33 - Display模块 相关概念解析
- Record one time of Tencent Test Development Engineer's automation interface test practice experience
- tomorrow! "Mobile cloud Cup" competition air publicity will start!
- Solr search
- Xshell传输文件
猜你喜欢

1285_把AUTOSAR函数以及变量等定义的宏用脚本展开以提高可读性

【Mask-RCNN】基于Mask-RCNN的目标检测和识别

汇编语言学习一(有栈协程铺垫,32位寄存器和相关指令学习,未完待续06/29)

相关数据库问题提问。

QT generate random number qrandomgenerator

Win10踩坑-开机0xc0000225

SOC_SD_CLK

Deploying web projects using idea

RT thread migration to s5p4418 (III): static memory pool management

ROS service communication programming
随机推荐
Google Earth engine (GEE) - Murray global tidal wetland change V1 (1999-2019) data set
Linux服务器安装Redis
SOC_AHB_SD_IF
[Hot100]10. Regular Expression Matching
Linux服務器安裝Redis
Egret P2 physical engine (1) small ball falling demo
oracle
Jingwei Hengrun won the 10ppm quality award of paccar group again
[Hot100]回文子串 与 最长回文子串
Egret P2 pit encountered by physical engine (1)
Ffmplay is not generated during the compilation and installation of ffmpeg source code
2021-07-02
MySQL Optimization: from more than ten seconds to 300 milliseconds
leetcode:98. Validate binary search tree
0 basic job transfer software test, how to achieve a monthly salary of 9.5k+
【已解决】ERROR 1290 (HY000): Unknown error 1290
IDEA import导入的类明明存在,却飘红?
2022年6月29日--使用C#迈出第一步--使用 C# 中的“if”、“else”和“else if”语句向代码添加决策逻辑
Ls1028 manual
What if I forget my account number after opening an account? Is it safe to open an account online?