Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Go language Technology

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to use Go language technology". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Why is it the characteristic of Go-Go

Go provides several basic but non-essential types, such as slices, interfaces, and channels.

Go simplicity is not its main selling point, as a static language, Go is as flexible as many dynamic scripting languages is the main selling point of Go. Saving memory, fast program startup and fast code execution together is another major selling point of Go. Go is a compiled and static programming language. Go was born in Google Research.

Built-in concurrent programming support:

Use goroutine as the basic computing unit. Easily create a collaborative process.

Channels (channel) are used to achieve synchronization and communication between protocols.

Mapping (map) and slice (slice) types are built in.

Polymorphism (polymorphism) is supported.

Use the interface (interface) to implement value boxing and reflection.

Pointers are supported.

Function closures (closure) are supported.

Support method.

Deferred function calls (defer) are supported.

Type embedding (type embedding) is supported.

Supports type inference (type deduction or type inference).

Memory is secure.

Automatic garbage collection.

Good code cross-platform.

The length of compilation time is an important factor in the pleasure of development. Short compilation time is one of the reasons why many programmers like Go

Run the first go program # # run the go program

Go run

# # package go programs and generate executable files

Go build

# # to install the latest version of a third-party Go program (to the GOBIIN directory) before the Go official toolchain version 1.16, the corresponding command was go get-u example.com/program (now obsolete and no longer recommended

Go install

# # check for possible code logic errors

Go vet

# # is it not as common as go mod tidy to add, upgrade, downgrade or delete individual dependencies to obtain network dependency packages?

Go get-u

# # dependent modules for generating go.mod files

Go mod init Demo.go

# # scan all code in the current project to add unrecorded dependencies to go.mod files or remove dependencies from go.mod files that are no longer used

Go mod tidy

# # formatting Source File Code

Go fmt Demo.go

# # running Units and benchmark Test cases

Go test

# # View the documentation of the Go code library package

Go doc

# # run go help aSubCommand to view the help information of a subcommand aSubCommand

Go help

GOROOT and GOPATH

GOROOT is the installation path of the Go language environment. When installing the development environment, it has been determined that GOPATH is the development path of the current project project. There can be multiple GOPATH. There are usually three packages under each GOPATH. Pkg, src and bin,src are used to store the source code files of the project project. The files under the pkg folder are automatically generated at compile time, and the executable files of * .exe are generated under the bin directory. PS: there can be three folders of pkg, src and bin under each GOPATH. When setting multiple GOPATH, the compilation results of the current GOPATH's src source files and the generated executable files will be stored in the pkg and bin folders of the nearest path, that is, under the current GOPATH. When developing, create a new directory under the src directory and establish the source code files, and the directory name and source file name can be different. The pkgName in the first line of code package pkgName in the source file can also have a different name from the folder where the source file is located. However, if this package needs to be used in other packages, the compiler will report an error, and it is recommended that the name after package is the same as the name of the folder where the file is located. Generally speaking, it is only under the source file where the main function is located that the package name is different from the package name declared by the "package package name".

The final import package needs to be written in package instead of the directory name, otherwise an error will be reported.

25 keywords break default func interface select of Go

Case defer go map struct

Chan else goto package switch

Const fallthrough if range type

Continue for import return var

Const, func, import, package, type, and var are used to declare various code elements.

Chan, interface, map, and struct are used in literal representations of some combined types.

Break, case, continue, default, else, fallthrough, for, goto, if, range, return, select and switch are used in process control statements. See the basic process control syntax for details.

Defer and go can also be seen as process control keywords, but they have some special functions. For more information, see Synergy and deferred function calls.

Ps: the size of values of types uintptr, int, and uint depends on the specific compiler implementation. Typically, on 64-bit architectures, the values of int and uint types are 64-bit; on 32-bit architectures, they are 32-bit. The compiler must ensure that the value of type uintptr can hold any memory address.

Constant automatic completion and iota

In a constant declaration that contains multiple constant descriptions, with the exception of the first constant description, all subsequent constant descriptions can have only the identifier part. The Go compiler will automatically complete the incomplete constant description by copying the complete constant description immediately preceding it. For example, during the compilation phase, the compiler will put the following code

Const (

X float32 = 3.14

Y / there must be only one identifier here

Z / / there must be only one identifier

A, B = "Go", "language"

C, _

/ / the empty identifier in the previous line is required if

/ / the previous line is an incomplete constant description.

)

Automatic completion is

Const (

X float32 = 3.14

Y float32 = 3.14

Z float32 = 3.14

A, B = "Go", "language"

C, _ = "Go", "language"

)

Iota is a special well-known constant that is pre-declared (built-in) in Go. Iota is pre-declared as 0, but its value is not constant at compile time. When this predeclared iota appears in a constant declaration, its value in the nth constant description is n (starting at 0). So iota only makes sense for constant declarations that contain multiple constant descriptions.

The combination of iota and constant description automatic completion can sometimes bring great convenience to Go programming. For example, here is an example that uses these two features

Const (

Failed = iota-1 / / =-1

Unknown / / = = 0

Succeeded / / = 1

)

Const (

Readable = 1

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report