“This is the 8th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

Write at the front 👀

Today we mainly talk about the basic knowledge of Go ❣

Declaration and initialization

1. Variable declarations

  • A standard format

Var < variable name > < data type >

  var a int
Copy the code
  • Batch format

If you have a large number of variables to declare, you can use parentheses () for batch declarations

  var( a int
       b bool
       c string
 )      
Copy the code

2. Variable initialization and assignment

  • Declare a variable and assign it at the same time

Statement: var < variable name > < data type > = value

var a int =18
Copy the code
  • First sound, then assign
package main

import "fmt"

var (
	a int
	b bool                    // Declare variables first
	c string
)

func main(a) {
	a = 18
	b = true              // Assign a value to the variable
	c = "Stubborn horns."
	fmt.Println(a, b, c)
}

Copy the code
  • Automatic recognition by intelligent compiler

The Go compiler is smart enough to automatically infer the type of a variable based on its value, but it can’t do everything. For example, to set a specific data type such as int8, you need to use var a int8

   var( 
        a = 15
	b = true
	c = "Stubborn horns."
  )      
Copy the code
  • Short variable declaration

Statement: < variable name > := value Note: Short declaration syntax := can only be used in the body of a function, is a local variable; Variables like those defined with VAR are generally outside the function, global, and package level.

a := 18 // Note: The variable name to the left of the equal sign must be a variable name that has not been defined, otherwise a compilation error will occur!
Copy the code

3. The constant

Constants are defined with the const keyword to store data that does not change, so they can define only bools, numbers (integers, floating points, and complex numbers), and strings.

  • Standard definition format (explicit type definition)

Const < variable name > < data type > = value

const name string = "Stubborn horns."
Copy the code
  • Implicit type definition

A const < variable name > = value is also automatically determined by an intelligent compiler to determine the data type in the factory, but it is also not universal. To define a specific data type, the standard definition format must be used.

const name string = "Stubborn horns."
Copy the code
  • Constants are defined and initialized similarly to variables, but they cannot be used: =Syntax declaration.

Anonymous variables

As the name implies, a variable without a name, which is represented by the underscore _. Anonymous variables take no namespace or memory, and can be declared multiple times without conflict. You can think of it as a tool guy, and I’ll talk about his actual use in functions.

2. Basic data types

type Length (bytes) Zero value instructions
bool 1 false true flase
byte 1 0 uint8
rune 4 0 Unicode Code Point, int32
int, uint 4 or 8 0 32 or 64 bits
int8, uint8 1 0 -128 ~ 127, 0 ~ 255, byte is the alias of uint8
int16, uint16 2 0 -32768 to 32767, 0 to 65535
int32, uint32 4 0 – 2.1 billion to 2.1 billion, 0 to 4.2 billion, rune is an alias for int32
int64, uint64 8 0 3.4 e38
float32 4 0.0
float64 8 0.0 1.8 e308
complex64 8 The plural
complex128 16 The plural
uintptr 4 or 8 The value is a uint32 or uint64 integer of the storage pointer
string “” Utf-8 string
Type conversion

Unlike C, Go has only cast, not implicit cast.

Conversion format: T(expression) T indicates the data type to be converted

Write in the back

Thank you for watching ✨. If you have any questions, please point them out to 💖