当前位置:网站首页>Go language learning notes - analyze the first program

Go language learning notes - analyze the first program

2022-07-05 11:13:00 Bigbear

Analyze the first program

The first program :helloworld.go





1、 First line of code package main The package name is defined .
You have to indicate in the first line of the source file which package this file belongs to ,
Such as :package main.package main Represents a program that can be executed independently ,
Every Go The application contains a file named main My bag .

2、 The next line import "fmt" tell Go Compiler this program needs to use fmt package ( Function of , Or other elements ),
fmt Package implements formatting IO( Input / Output ) Function of .

3、 The next line func main() Is the function that the program starts to execute .
main Function is a necessary part of every executable program ,
Generally speaking, it is the first function executed after startup ( If there is init() Function will execute the function first ).

4、 The next line /*...*/ It's a comment , Will be ignored during program execution .
Single line comments are the most common form of comments , You can use it anywhere to // The opening single line comment .
Multiline comments are also called block comments , All of them have been /* start , And */ ending , And it can't be nested ,
Multiline annotations are generally used for package document description or block code fragments .

5、 The next line fmt.Println(...) You can output strings to the console , And at the end automatically add the line feed character \n.
Use fmt.Print("hello, world\n") You can get the same result .
Print and Println These two functions also support the use of variables , Such as :fmt.Println(arr).
If not specified , They print variables in the default print format arr Output to console .

6、 When identifier ( Include constants 、 Variable 、 type 、 Function name 、 Structure fields and so on )
Start with a capital letter , Such as :Group1, Then the object using this form of identifier
Can be used by external package code ( The client program needs to import this package first ),
This is called export ( Like in object-oriented languages public); If the identifier starts with a lowercase letter ,
It's invisible outside the package , But they are visible and available inside the entire package
( Like in object-oriented languages protected ).

Go Mark -Go A program can be made up of multiple tags , It can be a keyword , identifier , Constant , character string , Symbol .
As below GO Statement by 6 It's made up of two markers :

1. fmt
2. .
3. Println
4. (
5. "Hello, World!"
6. )

Line separator : stay Go In the program , A line represents the end of a statement .
Every sentence doesn't need to look like C Other languages in the family use semicolons as well ;
ending , Because these jobs will be done by Go Compiler autocompletes .
If you plan to write multiple statements on the same line , They have to use ;
Artificial distinction , But in the actual development, we do not encourage this kind of practice .
fmt.Println("Hello, World!")

notes : Comments are not compiled , Each package should have related comments .

Single line comments are the most common form of comments , You can use it anywhere to // The opening single line comment .
Multiline comments are also called block comments , All of them have been /* start , And */ ending . Such as :

// Single-line comments
/*
 Author by Novice tutorial
  I'm a multi-line comment
 */

 

More technology sharing concerns ->  

 

原网站

版权声明
本文为[Bigbear]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051059475907.html