first, filename & keyword & identifierAll go source codes start with a letter or an underscore, and case sensitive underline _ is a special identifier that can be set when the user ignores the result reserved keyword Import Package
The following are reserved keywords:
Ii. The basic structure of Go procedure
Package main
Import (
"FMT"
)
func main () {
fmt. Println ("Hello World")
}
Any code must be subordinate to a package import keyword, introducing other package Golang executables, package main and only one function in the main entry function package, which can be called directly in the same function, and in the same functions, the functions in different packages are called by the package name + dot + function name. Packet access control rules, uppercase means that the function or variable can be exported, and in other packages can be invoked, lowercase thought that this function or variable is private, the package can not be accessed outside.
Little Exercise 1
Write a small program, for a given number n, to find all 22 add equals N of the combination
Package main
Import (
"FMT"
)
func add_num (n int) {for
i:=0;i<=n;i++{
FMT. Printf ("%d+%d=%d\n", I,n-i,n)
}
}
func main () {
add_num (5)
}
Little Exercise 2
Write a small program that contains two packages, add and main, where there are two variables in the Add Package: Name, and age, and how to access name and age in the main package. (used to understand the case sensitive problem in Go)
The code in the main package:
Package main
Import (
"FMT"
"Go_dev/day02/kexia02/add"
)
func main () {
fmt. Println (Add. Name)
}
Code in the Add package
Package Add
var Name string = "Zhaofan"
var age int = 23
From the results we can see that we cannot call the age in the add package in the main package, but it is possible to call to the name
This is because of the case, uppercase in go can be understood as public in other languages, lowercase is understood as private
Here's a question to note:
We'll change the code in the Add package to:
Package Add
var name string
var age int
Name = ' Zhaofan ' Age
= 23
This is also the wrong way of writing, go as a compiled language, you must use the function to execute the statement, but not outside the function to execute the statement
Little Exercise 3
Develop a small program that uses package aliases to access functions or variables in the package
Make changes directly to the main package of the previous program
Package main
Import (
"FMT"
a "Go_dev/day02/kexia02/add"
)
func main () {
fmt. Println (a.name)
FMT. Println (a.age)
}
Little Exercise 4
Each source file can contain an init function, which is automatically invoked by the Go run framework, as shown in the following example:
Package main
Import (
"FMT"
)
func init () {
fmt. PRINTLN ("Execute initialization function")
}
func main () {
fmt. Println ("Hello World")
}
The result is that the init function is printed first, and then the problem is printed in the main function, so the INIT function performs three functions with the main function declaration and Comment
function declaration
Format is: Func function name (argument list) (return value list)
Examples are as follows:
Func Add () {
}
Func Add (a int,b int) int{
}
Func Add (a int,b int) (int int) {
}
Comments
Single-line Comment//
Multiline comment/* /Four, go language data type and operator
Common data types and classifications
The go language is divided into several types of data by Category:
Boolean: True or False, example: var b bool = True
Number types: include int and float float
String type: Here it is emphasized that the go string is a byte of the go string concatenated by a single byte to identify the Unicode text using the UTF-8 encoding.
Derived type: This includes pointer type, array type, structured type, channel type, function type, interface type, map type
Attention:
String with double quotes "", here you can also use inverted quotes ', which, by way of inverted quotes, preserves your formatting and does not escape any of your content.
byte when using single quotes ', you can also use inverted quotes '
var cc byte = ' C ' fmt.println (CCC)
var cc byte = C fmt.println (cc)
One will print C's ASCII, one will print C
About FMT. Usage of Printf ()
Website address: https://go-zh.org/pkg/fmt/
So so
%v the default format for the corresponding value. When you print the structure body, the plus sign (%+v) adds the field name
% #v The go syntax for the corresponding value
The go syntax for the type of%T corresponding value is represented
Percent% literal, not a placeholder for values
Boolean
%t word True or false.
Integer
%b binary representation
The character represented by the corresponding Unicode code point in%c
%d decimal representation
%o Octal notation
%q the character literal value around the single quotation mark, which is safely escaped by the go syntax
%x hexadecimal notation, alphabetic form lowercase a-f
%x hexadecimal notation, alphabetic form uppercase A-f
%u Unicode format: u+1234, equivalent to "u+%04x"
Floating point numbers and their composition
%b A scientific notation of the power of exponent two, with StrConv, without a decimal part. Formatfloat
The ' B ' conversion format is consistent. such as -123456p-78
%e scientific counting method, for example -1234.456e+78
%E scientific counting method, for example -1234.456E+78
%f has a decimal point but no index, for example 123.456
%g Select%e or%f as appropriate to produce a more compact (no end 0) output
%G Select%E or%f as appropriate to produce a more compact (no end 0) output
String and byte slices
No translatable bytes for%s string or slice
%q a string surrounded by double quotes, safely escaped by go syntax
%x 16, lowercase letters, two characters per byte
%x 16, uppercase letters, two characters per byte
Pointer
%p hexadecimal notation, prefix 0x
Through the FMT. Printf () can format the output to the terminal, and if you want to format the stored to the variable is FMT. Sprintf ()
Number Type
The number types include:
Uint8 (unsigned 8-bit integer, 0 to 255)
UInt16 (unsigned 16-bit integer, 0 to 65535)
UInt32 (unsigned 32-bit integer, 0 to 4294967295)
Unint64 (unsigned 64-bit integer, 0 to 18446744073709551615)
Int8 (signed 8-bit integer,-128 to 127)
Int16 (signed 16-bit integer,-32768 to 32767)
Int32 (Signed 32-bit integer,-2147483648 to 2147483647)
Int64 (signed 64-bit integer,-9223372036854775808 to 9223372036854775807)
Floating-point type
FLAT32:32-bit floating-point number
flag64:64-bit floating-point number
complex64:32 digits and imaginary numbers
complex128:64 digits and imaginary numbers
Type conversions
For example to understand: var a int = 8 is converted to int32 var b int32 = Int32 (a)
When we design the data in our code, we have to ensure that two data types are identical.
var a int and Var b int32 cannot be computed directly, and this time you need to use a type conversion
Related operators
。 && | | To denote a non, with, or
= =, =,!=, <, >, <=, >=
Exercise 1
Generate random integers using Math/rand, 10 random integers less than 100, and 10 random floating-point numbers
Package main
Import (
"FMT"
"Math/rand"
)
func rand_print () {for
i:=0;i<10;i++{
FMT . Println (Rand. int31n ())
FMT. Println (Rand. Float32 ()}
}
func main () {
rand_print ()
}
But here the random numbers generated by each run program are the same, and the solution is to add random seeds: Rand. Seed (time. Now (). Unix ()) V, constant
Commonly used cost modifiers, which represent always read-only and cannot be modified
Const can only modify Boolean,number (int related type, floating-point type, complex) and string
Grammar
const variable name [Variable type] = value where the variable type can be omitted
Example
Const B string = "Hello"
const B INT = 23
Usually define the illuminated steady.
Const (
A = 0
b = 1
)
Advanced methods
Const (
A = Iota
B
C
)
It's automatically back here. A assignment is 0, and the following variable is added 1 at a time.