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

What are the ways of declaring Go variables

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

Share

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

This article mainly explains "what are the ways of Go variable declaration". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the ways of Go variable declaration".

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 the go program and generate the executable file go build # # to install the latest version of a third-party Go program (to the GOBIIN directory) before version 1.16 of the official Go tool chain The corresponding command is go get-u example.com/program (now deprecated and no longer recommended to use go install # # check for possible code logic errors go vet## to get the network dependency package, use pull to add, upgrade, downgrade or delete individual dependencies, not as common as go mod tidy? Go get-uplink # generate go.mod file Dependent module go mod init Demo.go## scans all code in the current project to add unrecorded dependencies to the go.mod file or to remove from the go.mod file dependencies that are no longer used dependent go mod tidy## formatted source file code go fmt Demo.go## run unit and benchmark test case go test## view the documentation of the Go library package go doc # # run go help aSubCommand to View the help information for a subcommand aSubCommand go helpGOROOT 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 selectcase defer go map structchan else goto package switchconst fallthrough if range typecontinue for import return var of Go

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.14Y / / there must be only one identifier 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.14Y float32 = 3.14Z float32 = 3.14A, 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