1. Two ways to declare variables


The first way to declare a variable is through var:
Example: var name string or var name = “nshu” this approach exploits the Go language’s own type inference.
The second way is through the short variable declaration method:
Example Name: = “nshu” this method can only be used inside a function body


2. Benefits of go language type inference


The language type inference can significantly improve the flexibility of the program, made it easier to code refactoring, because when you modify functions, changed the function return value type, but the inside of the main function assignment is accomplished by language type inference, so don’t need to modify the main function, that is why the code maintenance is also becoming more Suggestions, And because GO is statically typed, its type inference is implemented at compile time, unlike its dynamic counterparts, which sacrifice program maintainability and runtime efficiency.


3. The code block


In Go, a code block is usually an area surrounded by curly braces. The Go language itself and the code we write together form a large code block, also known as a global code block. As long as the global variable is exposed, it can be used by all code, compared to the smaller code block is code package, the smaller is a source text
And finally, functions like if, swich, select and so on.


4. Variable redeclaration


Since the type of a variable is already determined when it is initialized, redeclaring it must assign the same type as the original, or it will generate a compilation error.
Redeclarations of variables can only occur within the same code block. If the name is the same as that of a variable in the outer code block, overwriting of the variable occurs in this code block, but the value of the outer variable does not change in the outer code block.
A redeclaration of a variable only happens when a short variable declaration is used, otherwise it will not compile. For example,
Var name = “nshu” var name = “nshu”
Var name = “nshu” name := “nshu”


5. How to determine the type of a variable


Type assertion expressions can determine the type of a variable, for example: value, OK :=interface{}(container).([]string)
The type assertion expression includes inertface{}(container), which converts the value of the container variable to an empty interface value, and a.(string) that determines whether the former is of type slice [] String. The result of this expression can be assigned to two variables, Represented here by OK and value. If ok is a bool, it will represent the result of the type. If true, the evaluated value will be converted to []string and assigned to value, otherwise value will be given nil.
Note that “OK” will be added to the assignment otherwise the program will panic if it evaluates to “no”.
Type assertion expressions are of the syntactic form x.(T). X can only be an interface type. In Go interface{} is an empty interface and any type is its implementation type.


6. What happens if a variable has the same name as its outer variable


Go language variable lookup process: first, the code reference variables when you first find the variables in the current code block, secondly if no variable declaration in the name of the current code block, the program will be along the code block nested relations, from the current block of code to the outer layers of search, until you find the variable, if can’t find the compiler complains.
Note that references to program entities that import other code packages in the current source file need to be prefixed with qualifiers. Because without the qualifier the program doesn’t look in the package that was introduced. Except for the form (import.xxx).
Using import.xxx as stated in the article, it is basically assumed that the imported code package, as in this package, is in fact the same scope, and naturally does not allow repeated declarations.


7. The difference between a named variable in a different code block and a variable in a variable redeclaration
A variable in a variable redeclaration must be within a code block, while a variable with a same name in different code blocks is a variable represented by the same identifier between multiple code blocks.
Variable redeclarations refer to multiple declarations of a variable, whereas identical variables in different code blocks refer to multiple variables.

No matter how many times variables are redeclared, their type must always be the same as when they were first declared. Namable variables do not have to follow this rule

Duplicate variables in different code blocks are shielded between different code blocks, and variable redeclarations do not occur.



6. Type conversion needs to be noted


First, the type conversion of integer values and integer constants is legal in principle as long as the source value is within the representable range of the target type.


For example, uint(255) can convert the untyped constant 255 to a value of the uint8 type because 255 is in the range of [0,255]
But be careful when the representable range of the source number type is large and the representable range of the target type is small.
Example:
package main


import (
“fmt”
)


func main() {
var a int16 = -255
b := int8(a)
fmt.Println(b)
}
The output is 1
Resolution:
The Go language and computers are all stored in the form of complement. This is mainly to simplify the process of computing integers.
The source code is the sign bit plus the absolute value of the truth value, that is, the first bit represents the sign and the remaining bits represent the value.
The expression of the inverse code is: the inverse of a positive number is itself; The inverse of a negative number is based on its original code, the sign bit is unchanged, the rest of the bits are inverse.
The expression of complement is: the complement of a positive number is itself; The complement of a negative number is based on its original code, with the same sign bits, the other bits reversed, and finally +1. (i.e. +1 on the basis of inverse)
In this case, the complement of the int16 value 255 is 1111111100000001. If we convert the value to int8, Go will truncate the higher octet, resulting in 00000001. And because its sign bit is 0, it means that it is a positive integer. Since the complement of a positive integer is equal to its source code, the final value of B is 1.
When the valid range of integer values is narrowed from wide, only a certain number of high binary values need to be truncated in the complement form. When converting a floating point number to an integer, the decimal part of the former is truncated entirely.


Second, although it is possible to convert an integer value directly to a string value, the converted integer value should represent a valid Unicode code point, otherwise the result will be � �. � The Unicode code point is U+FFFD. It is a Replacement Character defined in the Unicode standard to replace characters that are unknown, unrecognized, and undisplayable.
Third, the interdependencies between strings and various types. When a value is converted from string to []byte, it represents that the UTF-8 encoded string is broken up into discrete bytes. A single byte encoded in UTF-8 cannot represent a character, except for the portion of the character set that is compatible with ASCII.
Such as:

String (byte [] {‘ \ xe4 ‘, ‘\ XBD’, ‘\ xa0’, ‘\ xe5’, ‘\ xa5’, ‘\ XBD’}) / / hello

The utF-8 encoded three bytes \xe4, \ XBD, and \xa0 together represent the character ‘you’, while \xe5, \xa5, and \ XBD together represent the character ‘ok’.
Second, a value that is converted from string to []rune represents the splitting of the string into Unicode characters.
String ([]rune{‘\u4F60’, ‘\u597D’}) // Hello


7. Type aliases, potential types




We can declare various custom types with the key value type. These types must fall within the scope of the Go language primitive and advanced types. We can declare it like this:


type Mystring = string


This statement means that MyString is an alias type of string. It means that the two types differ only in name, they are identical. Two alias types exist in the Go language’s built-in primitive types. Byte is the uint8 alias type and rune is the Int32 alias type. But if it is stated like this:


type Mystring2 string


Mystring2 and String are two different types. Here Mystring2 is a new type, unlike any other type, in a way called type redefinition, where string can be called the underlying type of Mystring2. The implication of a potential type is what type or collection of types a type is essentially. Values of different types potentially of the same type can be converted to each other, so Mystring2 and String can be converted to each other. But []Mystring2 and [] String cannot be converted to each other before, because their underlying type is Mystring2 String, respectively.


Note that no comparison can be made between two types of the same underlying type.