当前位置:网站首页>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 !
边栏推荐
- [daily question] 535 Encryption and decryption of tinyurl
- 成品升级程序
- Rising posture series: fancy debugging information
- Definition and use of ROS topic messages
- 0 basic job transfer software test, how to achieve a monthly salary of 9.5k+
- Redis cache
- QT generate random number qrandomgenerator
- SOC_ AHB_ SD_ IF
- Write a C program to judge whether the system is large end byte order or small end byte order
- 如果我在珠海,到哪里开户比较好?另外,手机开户安全么?
猜你喜欢

app闪退

第一行代码(第三版)学习笔记
![[mask RCNN] target detection and recognition based on mask RCNN](/img/80/f3db990b4f242609679d872b0dfe00.png)
[mask RCNN] target detection and recognition based on mask RCNN

leetcode:98. Validate binary search tree

MySQL Optimization: from more than ten seconds to 300 milliseconds

Jingwei Hengrun won the 10ppm quality award of paccar group again

SOC_SD_CLK

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

Relevant database questions.

编写并运行第一个Go语言程序
随机推荐
【已解决】Failed! Error: Unknown error 1130
Redis cache
failed to create symbolic link ‘/usr/bin/mysql’: File exists
tomorrow! "Mobile cloud Cup" competition air publicity will start!
安装Go语言开发工具
踩坑记录:supervisor 日志返回信息:redis扩展未安装
Imxq Freescale yocto project compilation record
Porting RT thread to s5p4418 (V): thread communication
Write a C program to judge whether the system is large end byte order or small end byte order
Cluster distributed
SOC_ AHB_ SD_ IF
[fuzzy neural network] mobile robot path planning based on Fuzzy Neural Network
【已解决】MySQL异常:ERROR 1045 (28000): Unknown error 1045,忘记初始密码
记录一次腾讯测试开发工程师自动化接口测试实践经验
[my advanced OpenGL learning journey] about the access methods of vector and matrix classification of OpenGL shaders: xyzw/rgba/stpq and array subscripts
Linu基础-分区规划与使用
Ftplib+ tqdm upload and download progress bar
Solr search
What if I forget my account number after opening an account? Is it safe to open an account online?
[semidrive source code analysis] [x9 chip startup process] 33 - Analysis of related concepts of display module