1. Import the package. There is only one main function in a main function, which contains one main package. In the main function, we need to declare the main package!
package main
func main(){
}
2. Introduce package files, for example, we use the fmt package, and print helloword. Golang has a definition of package visibility, such as private and protected declarations in other languages. When handwritten letters are lowercase, they are available in the package, but they are not available outside the package. When handwritten letters are capitalized, they can be used in reference to the package! For example: ();We will find that the functions we use and referenced are both capitalized! Lowercase will call incorrectly!
package main
import “fmt”
func main(){
("Hello, me");
}
3. The obfuscation mechanism of the package, we can package alias, for example, if we feel that the package cannot be remembered or the package name is almost the same, we use this alias mechanism!
package main
import str "fmt"
package main(){
("Hello, me");
}
4. Abbreviation of multiple packages, constants, global variables, general types, etc. How to declare when we have multiple constants, packages, etc.
import( const ( var(
“fmt” PI =”3.1415926” name =1
“net” NAME =”widuu” age =12
“io” AGE =”12” wo =”me”
) ) )
Types of
bool Boolean type has true false There is no 0 and 1
int Integer Customized according to the platform If you are 32-bit, it is 32-bit integer
int8 8-bit integer in the value range of -128~256. In fact, the value is at the power of 2 to 8. Signed means negative numbers, and uint8 means unsigned
int16 16-bit integer
rune �
int64 �
float32/64 bit
complex64 /complex128 Negative number type
Other types
array slice string
Reference Type
slice map chan
Interface type interface
Function type func
5. Variable declarations and assignments and declared values
We declare that when there is no assignment, we will find this
When declaring int float output is 0, declaring bool output is false
The reference package math outputs the maximum value and minimum value, such as the value range of the maximum output int16 (math.MaxInt16)
6. Type declaration We can customize the type declaration, such as the following
package main
import std "fmt"
type text string
func main() {
var a text
a = "Hello"
(a)
}
7. Type conversion
View source code printing help
var a float32 = 100.01
b:=int(a) //The value of output a is 100.01 The value of output b is 100
var c int = 65
d:=string(c) //The output is A, which is directly converted into text form A
b:=(c) //This output is 65 direct text output