Statically typed language: Just the opposite of dynamically typed language, its data type checking occurs in the compiling stage, which means that the data type of the variable must be declared when writing the program. C/C++, C#, and Java are all typical representatives of statically typed languages.
Dynamically typed language: refers to a language that does data type checking during runtime. When programming in a dynamic language, you don't need to assign a data type to a variable. The language will record the data type internally when you assign a value to the variable for the first time. Python and Ruby are a typical dynamically typed language
Strongly typed language: A language that makes it mandatory for data type definitions. Before mandatory type conversion, two different types of variables are not allowed to operate mutually. Strongly typed languages are type-safe languages, such as Java, C# and Python. For example, "int i = 0.0;" in Java cannot be compiled
Weakly typed languages: languages whose data types can be ignored. Contrary to strongly typed languages, a variable can be assigned values of different data types, allowing a block of memory to be treated as multiple types, such as directly adding integer variables to character variables. C++ and PHP are weakly typed languages. For example, "int i = 0.0;" in C++ can be compiled and run;
It can be compiled across platforms, and programs running on Linux can be compiled under windows, that is, executable files for another platform can be compiled on this platform.
Go can be cross-platform compiled, python needs to install python interpreter and the way to install python interpreter on different platforms is still different, java needs to install java virtual machine (jdk) different platform installation methods are also different, c/c++ needs to be in Compile on-site on different platforms, so the advantages of go are highlighted. Using go, you can write it on your computer and directly compile it into a file that can be executed by the server and run it, eliminating the cumbersome steps of setting up the environment.
Features of go: garbage collection mechanism, support for object-oriented and process-oriented (go is not a pure object-oriented language, there is no concept of classes, but it can achieve object-oriented features: inheritance, encapsulation, polymorphism)
The source code of go was written in c. After 1.5, I wrote my own. This is similar to python. The python source code was written in c/c++ at the beginning, and then I wrote my own.
Current use of go: All major Internet companies use go for operation and maintenance and back-end. The more popular k8s (container orchestration) is written in go. Since the first open source project of the blockchain is written in go, Go is also used in blockchain.
At present, the disadvantages of go are the new language and few modules.
Go appeared late and has inherent advantages that other languages do not have: it is more suitable for multi-core operation of modern processors, and programming also combines the advantages of the previous programming languages. Some people say that go will dominate the programming language for the next ten years.
1. install the development environment
2. IDE installation
Note: go and python are different, the code of go has nothing to do with indentation.
int : The default is the number of execution bits of the computer, such as int32 for a 32-bit computer,
int8 : The storage length is 8 bits, the first digit indicates the sign, the last seven digits indicate that the content can indicate positive 2^7-1, the same is true below
int16
int32
int64
uint8 : All eight bits represent content, that is, it can represent [0,2^8-1), so unit8 is aliased to byte (byte)
uint16
uint32 : alias rune, 4 bytes, representing one character
uint64
float32
float64
complex32
complex64
string: The double quotation mark is a string. Note that it must be double quotation marks. Single quotation marks and triple quotation marks will not work. Single quotation marks will print out the corresponding ASCII code numbers of the characters quoted inside. Backticks are strings that support line breaks.
The bool type here only has two states: true and false
0, 1 has nothing to do with bool
The constants here cannot be changed, and an error will be reported if they are modified, so they cannot be simply declared. Usually used to define the database connection address, port number, etc.
package main import "fmt" const name = "zgh"//constant definition const name1,age = "zgh",20//define multiple constants at once func main(){ }
Single-line comment //
multi-line comment/*要注释的内容*/
Go's annotation method is inherited from C, except for the C language JavaScript also uses this annotation method.
package main//which package must be specified main package //It means import the fmt package and compare with python import "fmt" func main() {//Define a main function fmt.Println(a..."hello world")//Print out helloworld } /*note: 1. The code of go must be written in the main function. The entry of the go program is the main function under the main package, which is similar to the C language 2. fmt.Println("hello world") The a... you see is actually prompted by goland. The formal parameter of the function is a..., there is no a... in other IDEs 3. In go language, package import must be used, otherwise an error will be reported, commented out, and the package will be automatically deleted (made by goland, other editors, no) 4. The name of the go file cannot be the same as the function name or package name such as the main function, otherwise an error will be reported */
Let's compare the C language
#include <stdio.h> int main() { /* My first C program*/ printf("Hello, World!/n"); return 0; } /* All C language programs need to include the main() function. The code starts execution from the main() function. printf() is used to format the output to the screen. The printf() function is declared in the "stdio.h" header file. stdio.h is a header file (standard input and output header file), #include is a preprocessing command used to include header files. When the compiler encounters the printf() function, if it does not find the stdio.h header file, a compilation error will occur. return 0; statement is used to indicate exit from the program */
//Execute go code 1 Right-click Run on goland to execute (only for goland, no other editors such as vs code have run) 2 First compile, and then perform the orthodox usage (go is a compiled language), use the command Compile: go build s1.go compile, compile into the executable file s1.exe of the current platform Execution: s1.exe 3 Compile and execute (execute the go file in the command window on vs code) go run s1.go is equivalent to all the steps in 2 and does not generate executable file s1.exe
package main import "fmt" func main(){ //fmt.Println("hello world") //1 The first type: full definition //var keyword variable name variable type = variable value var a int = 10 fmt.Println("a:",a) //2 The second type: type deduction (automatically deduces the variable type) var b = 10 fmt.Println("b:",b) //3 The third type: abbreviated statement (this one uses a lot) colon and equal sign are together c := 10 fmt.Println("c:",c) //4 Only define variables without assigning values var d int fmt.Println("d:",d) //5 Declare multiple variables, all three ways are possible //var e,f,g int = 1,2,3 //var e,f,g = 10,11,"xxx" e,f,g := 10,11,"xxx" fmt.Println("e:",e,"f:",f,"g:",g) //6 Variables cannot be defined repeatedly //var h int //var h = 90//Repeated definition will report an error //h := 90//Repeated definition will report an error //h = 90//Repeated definition will report an error var h int //var h,i = 10,20//Repeated definition will report an error h,i := 10,20//allowed (special case) fmt.Println(h,i) } /* summary: 1 The variable is defined and must be used, otherwise an error will be reported (only go requires this) 2 View variables are not written 3 Variables must be defined before use 4 Variable types are fixed and cannot be changed midway (static language) 5 If you only define variables, you must specify the type, and you can only use the first definition method 6 Variables are not allowed to be defined repeatedly Emphasize: After all types of variable definitions refer to the first three ways of defining variables */
a: 10 b: 10 c: 10 d: 0 e: 1 f: 2 g: 3 e: 10 f: 11 g: xxx e: 10 f: 11 g: xxx