Preface:
In Go language, the declaration of variables is one of the basics when writing programs.
usevar
Keywords can define single or multiple variables, and it is possible to choose whether to initialize these variables.
The Go language's static typing system requires that the variable be specified when declaring a variable, but also provides type inference functionality so that type declarations can be omitted in some cases.
This article will introduce how to use itvar
Keywords are variable declarations and provide some examples to help understand.
Basic Type:
The basic types of Go language are:
- bool
- string
- int、int8、int16、int32、int64
- uint、uint8、uint16、uint32、uint64、uintptr
- byte // alias for uint8
- rune // The alias of int32 represents a Unicode code
- float32、float64
- complex64、complex128
When a variable is declared, the system automatically assigns it a zero value of that type: int is 0, float is 0.0, bool is false, string is an empty string, pointer is nil, etc.
All memory is initialized in Go.
Variable naming specification:
Some suggestions and specifications about Go variable naming:
-
Use meaningful names:
Variable names should clearly describe their purpose and meaning, avoiding single characters or vague naming.
-
Hump nomenclature:
In Go language, it is recommended to use camelCase to name variables, that is, the first letter of the first word is lowercase and the first letter of the subsequent word is uppercase, for example
userName
、totalCount
。 -
Avoid abbreviation:
Try to avoid using abbreviations, unless they are widely known abbreviations, which will reduce the readability of the code. For example, use
totalCount
ComparetotalCnt
It's easier to understand. -
Naming with noun:
Variable names should be nouns, not verbs, because variables are used to represent data or state.
-
Follow the agreement:
Follow the naming conventions of the project or team to maintain consistency in the style of code.
-
Avoid conflicts with keywords:
Do not use Go keywords as variable names to avoid confusion and errors.
-
Short and concise:
The variable name should be concise and clear, try not to be too long, but also ensure that it is clear enough.
-
Maintain consistency:
Maintain the consistency of variable naming throughout the project and avoid different naming styles.
Variable declaration
A variable declaration refers to explicitly telling the compiler in the program that an identifier is used as a variable and may give it an initial value.
The declaration of variables is usually to introduce new identifiers into the program and allocate storage space to store and operate data during program execution.
General syntax:
var identifier type // Variable declaration, not initializedvar identifier type = expression // Variable declaration and initialization
in:
-
var
is a keyword in Go language used to declare variables. -
identifier
It is the name of a variable and should comply with the naming rules. -
type
It is the data type of a variable, indicating the data type that the variable can store. -
expression
is the initial value (optional) of the variable, used to initialize the variable.
var age int // Declare an int type variable named age without initializingvar name string = "John" // Declare and initialize a variable of type string named namevar isStudent bool = true // Declare and initialize a bool type variable named isStudent
Brief syntax:
Go also provides a short statement syntax:=
, used to declare and initialize variables, it can declare variables more concisely, but can only be used inside functions. For example:
age := 25 // Shortly declare and initialize a variable called agename := "John" // Shortly declare and initialize a variable named name
For example:
In Go, usevar
Keyword declaration variables. Here are a few examples:
1. Declare a single variable:
var age int var name string var isStudent bool
2. Declare multiple variables:
var x, y int var x, y *int var name, email string var isActive, isAdmin bool
3. Bulk declaration of variables:
var ( a int b string c []float32 d func() bool e struct { x int } )
4. Declare and initialize variables:
var age int = 25 var name string = "John" var isStudent bool = true
5. Declare multiple variables and initialize:
var x, y int = 10, 20 var name, email string = "Alice", "alice@" var isActive, isAdmin bool = true, false
6. Short syntax assignment:
age := 25 // Shortly declare and initialize a variable called agename := "John" // Shortly declare and initialize a variable named name
In Go, if the variable has an initial value, the type can be omitted and the compiler infers the type based on the initial value:
var age = 25 var name = "John" var isStudent = true
Summarize:
The declaration of variables is one of the important concepts that every programmer must master when writing a Go language program.
By usingvar
Keywords, we can easily define and initialize variables, making our code clearer and easier to understand.
Mastering the basic syntax and best practices of variable declarations will help to write maintainable and efficient Go programs.
This is the end of this article about the declaration implementation example of Go language variables. For more relevant contents of Go language variable declarations, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!