An overview of the
The emergence of go is mainly to improve the development efficiency on the premise of ensuring c++ or Java performance. Here we can refer to What is the purpose of the project? .
The syntax of GO mainly comes from C family, on which a lot of simplification is made. For example, there is no class and simple concurrent API (Goroutine,channel), so the usage of Go is quite different from that of common languages. The following takes JS on nodeJS as an example. Do a basic understanding of GO through comparison.
Contrast with JS
Interpretation and compilation
Js is an interpreted language. The V8 engine in Node.js performs just-in-time (JIT) compilation in order to speed up the execution. Go is a compiled language.
type
Since the concepts of type systems are controversial (what is the difference between weakly typed, strongly typed, dynamically typed, statically typed languages?) , so there is no classification here. Js itself does not have static type checking, hence ts with type; There are various implicit conversions between types, so there are three equal signs that most languages don’t have. Although go can be declared without a type, it contains static type checking and cannot be converted implicitly. Type_name (expression) can be used to cast the type
object-oriented
Js supports object-oriented based on prototype inheritance, and sugar by the class syntax, in order to simplify the use of go, does not include class and other inheritance, Is not a real object-oriented language (Is go an object-oriented language?
multithreading
The official JAVASCRIPT specification ecMAScript doesn’t mention threads, but both Node. js and browsers have traditionally been single-thread-based asynchronous event-driven, but later the HTML specification introduced Web workers and Node. js introduced worker_Threads, Therefore, it also provides native support for multithreading, while GO specializes in high concurrency with lightweight thread Goroutines and messaging channels.
grammar
The syntax of JS is similar to that of C. The syntax of Go comes with lint features, such as the inability to import packages that are not used.
Package management
Js has the ecological flourishing NPM, which is the largest software Registry in the world, and YARN and NPM package management tools are very convenient. Go uses Go Modules, which can be used here.
structure
A basic GO code, for example
Package main import "FMT" func main() {/* This is my first simple program */ FMT.Println("Hello, World!" )}Copy the code
Contains the following sections
- Package declaration Declares the current package name
- Package introduced
- The startup function, main, is similar to the c language main function.
The data type
Includes basic data types and derived types, where the former includes
- The Boolean
- digital
- string
The latter include
- Pointer to the
- An array of
- slice
- structure
- function
- interface
- map
- channel
The Boolean
Boolean (bool) has only true and false values
Numeric types
Numeric types are very finely divided in GO, handling traditional ints, as well as long integers and floating-point numbers and complex numbers.
string
The string type is an array of characters, that is, []rune and []byte are quoted in double quotes. Rune and byte indicate a single character and are quoted in single quotes.
Pointer to the
Pointers in GO are neutered versions, which can be addressed with &, declared Pointers, and assigned to Pointers, similar to C except that operations cannot be performed
An array of
The array in GO and C language similar, used to store a certain type of fixed length, can be used… The length of the array is inferred by the number of elements, but the array name is a value type, not a pointer
var variable_name [SIZE] variable_type
Copy the code
slice
Is a dynamic array, which can be defined as an array of unspecified length
var identifier []type
Copy the code
Or use the make
var slice1 []type = make([]type, len)
Copy the code
structure
Again, it’s like C, using the struct keyword
type struct_variable_type struct {
member definition
member definition
...
member definition
}
Copy the code
Structure Pointers also use points to access members
Structures can be initialized directly, or new can be used, for example
type user struct {
id int `1123`
}
Copy the code
b := user{}
b.id = 222
c := new(user)
c.id = 333
Copy the code
function
The function is defined using the func keyword. The parameter list includes the parameter type, order, and number. The function name and parameter list form the function signature.
Func function_name([parameter list]) [return_types] {function body}Copy the code
Such as
func swap(x, y string) (string, string) {
return y, x
}
Copy the code
interface
Object-oriented languages such as c++ use classes to encapsulate data and methods, and in go you can use structures, interfaces, and functions and create a custom data structure. An interface type can define a set of methods, using the interface keyword, that other types implement as long as they have these methods. A structure can define a set of data and then encapsulate the data using functions to implement the corresponding methods of the interface. Such as
package main import ( "fmt" ) type Phone interface { call() } type NokiaPhone struct { } func (nokiaPhone NokiaPhone) call() { fmt.Println("I am Nokia, I can call you!" ) } type IPhone struct { } func (iPhone IPhone) call() { fmt.Println("I am iPhone, I can call you!" ) } func main() { var phone Phone phone = new(NokiaPhone) phone.call() phone = new(IPhone) phone.call() }Copy the code
map
A map is a dictionary that stores unordered key-value pairs
channel
The chan keyword is used to pass messages between two Goroutines.
Variables and constants
variable
There are many variable declaration methods
- Use var, where the type comes after the variable name, and if there is no type, the compiler will infer from the initial value that follows
var v_name v_type
v_name = value
Copy the code
If a type has no initial value, it is initialized to zero. Zero has different meanings for different types. Zero is initialized to 0 for numeric values, false for booleans, “” for strings, and generally nil
- Omit var and use
: =
Determine the type based on the following initial value, used in a function
v_name := value
Copy the code
- Multivariable declaration
Vname1, vname2, vname3 type vname1, vname2, vname3 = v1, v2, v3 var vname1, vname2, vname3 = v1, Vname1, vname2, vname3 := v1, v2, v3 // Variables that appear to the left of := should not be declared, Var (vname1 v_type1 vname2 v_type2) var (vname1 v_type1)Copy the code
constant
Constants are const and can only be primitives. Iota is a special constant
scope
It mainly includes functional scope and global scope
statements
Conditional statements
If statements, except for the parentheses can be omitted basically the same as JS
If Boolean expression {/* Executes when Boolean expression is true */}Copy the code
If Boolean expressions {/* execute when Boolean expressions are true */} else {/* Execute when Boolean expressions are false */}Copy the code
Switch: Each case is followed by a break by default. If not required, fallthrough can be used
switch var1 {
case val1:
...
case val2:
...
default:
...
}
Copy the code
The SELECT statement is the switch used in communication
Looping statements
A for loop can use a range to iterate over an array, slice, channel, or map. There are three forms. The first is the same as for in JS
for init; condition; post { }
Copy the code
The second is the same as while in JS
for condition { }
Copy the code
The third one is an infinite loop similar to for {}
for { }
Copy the code
When using range, use strings as an example
strings := []string{"google", "runoob"}
for i, s := range strings {
fmt.Println(i, s)
}
Copy the code
I stands for index and S stands for value
The end