What is a variable

A variable is a name given to a memory location to store a value of a particular type

There are various syntax for declaring variables in GO.

So a variable is essentially a small piece of memory that stores data that can be changed during the course of a program.

Variables are kind of the basic building blocks or building blocks of the programs that we write.

How do we get the data that we need into the computer, where we introduce variables

Conclusion:

  • A variable is a chunk of memory
  • Apply for storage space by defining variables

Function: Through the operation of variables to achieve the operation of the data we care about Application scenario: in the computer to store the data we need, this time we need to use variables

How to Use variables

The var name type is the syntax for declaring the inclusion of a single variable

Consists of one or more letters, digits, and underscores, starting with a letter or underscore

  1. No assignment has a default value of zero (zero is determined by data type)

    • If no value is assigned, the default value is used
    • Determine the variable type based on the value
var value1 int


var name type
name = value
Copy the code
  1. Brief statement
Value2 := ""Copy the code

This method can only be used in function bodies, not in global variable declarations and assignments

Such as:

Package main var a = "hello" var b string = "world" var c bool func main() {println(a, b, c)}Copy the code
  1. Direct assignment
Var value3 = 100 // In this case, the compiler automatically does type derivationCopy the code
  1. Multivariable declaration
  • The declaration and assignment are separated by commas. If no value is assigned, the default value exists
var value1, value2, value3 type
value1, value2, value3 = v1, v2, v3
Copy the code
  • Direct assignment, the following variables can be of different types
Var value1, value2, value3, value4, value5 = 1, "小 小 小 ", 2.3, false fmt.Println(value1, value2, value3, value4, value5)Copy the code
  • Collection types
var (
    value1 type1
    value2 type2
)
Copy the code
  1. Short statement, multiple assignments
Value1, value2, value3, value4, value5 := 1, "1 ", 2.3, falseCopy the code
  1. Apply multiple global variables, outside the function body
var (
    value int
   slices []int
   interfacex interface{}
)
Copy the code
  1. Exchange of variables
var i = 100
var j = 200
fmt.Println(i, j)
i, j = j, i
fmt.Println(i, j)
Copy the code

Matters needing attention

  1. Variables are not necessarily free of semicolons
  2. Go will report an error at compile time for variables that are declared but not used
  3. _Underline is a special variable name, and any value assigned to it is discarded
_, x := 1, 2
fmt.Println(_)
fmt.Println(x)
Copy the code

  1. Variable access control

    • Capital letterThe variables of class are extibable, that is, they can be read by other packages (described later), and are public (equivalent to the public permission modifier for class in traditional programming languages)
    • Lowercase beginningIs non-exportable and is a private variable that only this package can use (equivalent to the private permission modifier for class in traditional programming languages)
  2. Variables must be defined before they can be used

  3. Go is a static language that requires the same type of variable and the same type of assignment

  4. Variable names must not conflict. (No conflicts within the same scope)

  5. Short definition, at least one of the variable names on the left is new

  6. Short definition. Global variables cannot be defined.

  7. The zero value of the variable. Also called the default value.

  8. Use the variable once it’s defined, otherwise it won’t compile.

  9. We cannot use the initialization declaration again for variables with the same name in the same block of code

  10. If you use the variable a before defining it, you get a compilation error – undefined: a

  11. If you declare a local variable but do not use it in the same block of code, you will also get a compilation error

The last

Just learn Golang, hope everyone supervise and work together

Interested partners, welcome to follow my subscription number: EntrepreneurialG