Minimum Structure
Package main//Declaration Pack--No semicolon, the compiler level automatically adds a semicolon, indicating which package imports the function in the file
(//import Package--the package that needs to be used in the program needs to be imported beforehand.) Import imported packages can be enclosed in parentheses, followed by parentheses with no semicolon
"FMT"//commonly used for input/output//
multiple packages with Direct line wrapping, without commas or
semicolons)
func main () {// All executable programs require a main function, the main function has no return type, and is in the main package
FMT. Println ("Hello Golang")//No Semicolon
}
Basic Features
Strongly typed language, which eliminates the need to deduce variable types at run time, so it is more efficient than other types of languages
var variable name variable type
Declare the variables must be used, in order to unify the style;
The left curly brace must be at the end of a line, not the beginning, for style unification;
All executable programs require a main function, and the main function has no return type
Functions can take parameters, and formal parameters are declared in the same format as variables
Implicit declaration of variables: variable name: = The expression compiler automatically infers the type of the variable
Functions in the same package name can be called directly to each other, omitting the name of the previous package, and if it is not a package, it is necessary to declare the package name, add the package name at the time of the call, and the package name. function name (argument list) and package name. Variable Name
Use the GOFMT tool to adjust the source format, use the-w option to write back to the source file language feature garbage collection – Automatically manage memory, just need new, do not worry about delete, in Go is make corresponds to new. Natural concurrency – Support concurrency at the language level, greatly improving development efficiency in the CPU multicore era; Goroutine lightweight threading, based on the CSP model. A go process can take full advantage of multicore features. redis-Single Process single-threaded – requires multiple instances; nginx– multithreading model; traditional concurrency-thread pool capacity is related to CPU cores, otherwise it increases CPU load; The Go language launches 16 or 64 physical threads in the runtime library. Goroute thread scheduling is implemented. Channel (pipe) – Similar to the pipe in Linux, used for communication between goroutine, for all types (also use global variables (not recommended), note the visibility within the package – case)
Package main
Import (
"FMT"
)
func main () {
Pipe: = Make (chan int, 3)//make similar to New/malloc for allocating memory
//chan indicates that a channel type of space is opened
//int indicates that the element inside the channel is of type int, 3 is the channel capacity
//exceeds the capacity to block, waiting for the channel to be consumed with extra space, The content will be written to channel
//By default, the blocked location is exposed prematurely (if there is no other goroutine, the run-time library of Go will think the deadlock deadlock)
pipe <-1
pipe <-2
Pipe <-3
var t1 int
T1 =<-pipe
pipe <-4
FMT. PRINTLN (t1)
FMT. PRINTLN (len (pipe))
}
4. Multiple return values
Func My_func (a int,b int) (int, int) {//Multiple arguments and return types are separated by commas with
sum: = (A + b)
avg: = (a+b)/2
return sum,avg//multiple return values between Comma separated
}
Use underscore to discard function return value package The concept of the application has a main package, the main package has only one main function; The application has and only one main package organizes the same functionality or function-related code in a directory, which is the package Go is a package that organizes code, and any source file must belong to a package. No package there is no other code-module division, and folder corresponding to the same folder of Go file package name must be the same packet can be other packages referenced by the main purpose of the package is to improve the reusability of the Code
Calc.go Package
Calc
func Add (a int, b int) int{
sum: = (A + b)
return sum
}
//main.so
pa Ckage main
import (
"FMT"
"Calc"
)
func main () {
sum: = Calc. ADD (3,5)
FMT. Println ("sum=%d", sum)
}
MicroServices: Each service is independent and does not depend on other module compilation settings good gopath environment variable for its own workspace, that is, the project source file directory or its upper directory default will be in the SRC directory below Gopath start to find go source file, So just start at the next level of SRC and specify that the EXE executable that is generated for the entire package to be compiled appears in the Gopath directory, named after the deepest directory name in Go build