Table of contents
- What is cloud native
- study method
- IDE configuration of go language VScode
- Things to pay attention to when writing go code
- some basic commands
- basic grammar
-
- Go language comes with native tests
- Go vet
-
- Print-format error, check for type mismatched print
- Boolean errors, check for expressions that are always true, false or redundant
- Range loop, for example, the main coroutine in the following code will exit first, and the go routine cannot be executed.
- Unreachable code, such as the code after return
- Other errors, such as variable self-assignment, error check lag, etc.
- Code version control
- fmt.Println(...) and fmt.Print()
- Development steps
What is cloud native
The ability to build and run large-scale applications in dynamic environments including public cloud, private cloud, hybrid cloud, etc.
• Cloud native is an idea and a collection of technologies and enterprise management methods.
• Technology Level
• Applications are designed from the ground up to run on the cloud.
• Cloud platform is based on automation system.
• Process level
• Based on DevOps, CI/CD
Based on multiple means
• Application containerization encapsulation;
• Service mesh;
• Immutable infrastructure;
• Declarative API.
The significance of cloud native
• Improves system adaptability, manageability, and observability;
• Enables engineers to make frequent and predictable system changes at minimal cost.
• Improve speed and efficiency, help business growth, and shorten I2M (Idea to Market)
study method
IDE configuration of go language VScode
I don’t use goland because I don’t have money for the time being + I don’t want to use piracy for the time being + I don’t want to add a few gigabytes of storage consumption to the computer for the time being + I am also using vscode to learn the Rust language.
There are many online tutorials here, so I won’t list them all. Here is a recommended environment configuration for developing Go language under [Golang] VsCode (super detailed explanation with pictures and texts)
Perfectly solved my trouble of running code with vscode.
Things to pay attention to when writing go code
- You must indicate which package this file belongs to on the first non-commented line in the source file. [Package main represents an independently executable program. Each Go application contains a package named main. 】
- The fmt package implements functions for formatted IO (input/output)
- func main() is the function where program execution begins. The main function must be included in every executable program. Generally speaking, it is the first function to be executed after startup (if there is an init() function, this function will be executed first).
- / ... / is a single-line comment starting with //; multi-line comments are also called block comments, which start with /* and end with */, and cannot be nested. Multi-line comments are generally used in package documentation. Describe or comment code snippets into blocks
- When identifiers (including constants, variables, types, function names, structure fields, etc.) start with an uppercase letter, such as: Group1, then objects using this form of identifier can be used by the code of the external package (client The end program needs to import the package first), which is called an export (like public in an object-oriented language); if identifiers start with a lowercase letter, they are not visible outside the package, but they are visible inside the entire package And available (like protected in object-oriented languages)
- The Go language does not support dynamic linking , so all dependencies will be compiled into the same binary file during compilation.
some basic commands
bug
start a bug report
build
compile packages and dependencies
clean
remove object files and cached files
doc
show documentation for package or symbol
env
print Go environment information
fix
update packages to use new APIs
fmt
gofmt (reformat) package sources
generate
generate Go files by processing source
get
add dependencies to current module and install them
install
compile and install packages and dependencies
list
list packages or modules
mod
module maintenance
run
compile and run Go program
test
test packages
tool
run specified go tool
version
print Go version
vet
report likely mistakes in packages
Specify the output directory.
go build –o bin/mybinary
.Common
environment variable settings are compiled for the operating system and CPU architecture.
GOOS=linux GOARCH=amd64 go build
Full support list.
$GOROOT/src/go/build/syslist.go
basic grammar
Here are the novice tutorials recommended .
Go language comes with native tests
import "testing"
func TestIncrease(t *testing.T) {
t.Log("Start testing")
increase(1, 2)
}
go test ./… -v
Run tests
The go test command scans all *_test.go
files ending with . The convention is to put the test code and the official code in the same directory .
For example, the test code of foo.go is generally written in foo_test.go
Go vet
Code static inspection to find possible bugs or suspicious constructs.
Print-format error, check for type mismatched print
str := “hello world!”
fmt.Printf("%d\n", str)
Boolean errors, check for expressions that are always true, false or redundant
fmt.Println(i != 0 || i != 1)
Range loop, for example, the main coroutine in the following code will exit first, and the go routine cannot be executed.
words := []string{
"foo", "bar", "baz"}
for _, word := range words {
go func() {
fmt.Println(word).
}()
}
Unreachable code, such as the code after return
Other errors, such as variable self-assignment, error check lag, etc.
res, err := http.Get("https://www.spreadsheetdb.io/")
defer res.Body.Close()
if err != nil {
log.Fatal(err)
}
Code version control
fmt.Println(…) and fmt.Print()
You can output a string to the console and automatically add a newline character \n at the end.
The two functions Print and Println also support the use of variables, such as: fmt.Println(arr). If not specified otherwise, they print the variable arr to the console in the default printing format.
Development steps
Directory Structure