go learn 02 basic knowledge

1. Annotation classification in golang

  • Line comment, shortcut key Ctrl+/, uncomment the same

  • Block comment, shortcut key Alt+Shift+A, uncomment the same 

All programming language comments are similar 

2. Variable definition, directly use var to receive, and go infers the variable type by itself. 

 In some programming languages ​​like C#, you need to specify the variable type when defining a variable, such as string msg="hello go", now you can directly use var to replace it, var is what your value type is, and what type it returns.

2.1 Variable scope 

  • Package-level variables are global variables that can be used in all methods of the current program.
  • Function-level variables are local variables that can only be used within the current method.

Note: In Go, the same variable name is allowed, but in other languages, such as C#, the same variable name is not allowed. And in Go, there are variable names with the same name, and the variable names at the function level have the highest priority.

 

  

2.2 Variable declaration method 

  • The var declaration method automatically infers the variable type according to the value, as described above
  • Short declarations, using the colon: declaration, also automatically infer the variable type

Note: 1. The variable name of the short declaration must be unique, and the same short declaration variable cannot and will not exist, because it will directly prompt an error, for example:

 

 2. The short statement must be defined and used in the function body, and cannot be defined and used at the package level (global).

 2.3 Combination of variables

Why variables are merged is to reduce the amount of code and be beautiful, and to achieve the same effect as defining multiple variables.

Example 1: When we need to define multiple short declaration variables, it is usually defined like this.

But it can be defined like this:

 Example 2: Normal multiple variables are generally defined like this

But actually, it can be like this, 

 

 Even importing multiple packages can be written in this form

 Note: The variables defined in the function body must be used. If not used, an error will be prompted

 

 2.4 Variable naming convention

  1. cannot be a keyword of go
  2. cannot start with a number
  3. Variable names can only consist of strings, numbers, and underscores
  4. Variable declarations use camel case specification, or multiple English combinations, for example: hello_world or helloWorld
  5. Variable names are case-sensitive, for example: Hello and hello are two different variable names

For details, go to the official website to learn  Go language tutorial | rookie tutorial (runoob.com) 

Guess you like

Origin blog.csdn.net/weixin_39237340/article/details/126022429