In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to understand Go language variables and basic data types". In daily operation, I believe many people have doubts about how to understand Go language variables and basic data types. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to understand Go language variables and basic data types". Next, please follow the editor to study!
Catalogue
I. basic introduction
1. Characteristics of Go
2. Common commands of Go
3 、 Hello Word
Second, variables
1. Definition method
Third, constant
IV. Basic data types
1. Number
2. Floating point (32 + 64 represents the length after the decimal point)
3. Boolean (Bool)
4. String
I. basic introduction
Go is a static (compiled) language, which is a weakly typed language different from an interpretive language (static: fixed types, strong types: direct operations are not allowed for different types)
For example, python is a dynamic strongly typed language.
1. Characteristics of Go
Cross-platform compiled language, cross-compilation
Pipe (channel), slice (slice), concurrent (routine)
There is a garbage collection mechanism
Support for object-oriented and process-oriented programming patterns (Go's object-oriented has no concept of classes)
2. The environment variable of the commonly used Go command go env / / go-GO111MODULE= / / is empty and does not use the MODULE mode-GOPATH=C:\ Users\ oldboy\ go / / code storage path-GOROOT=c:\ go / / go sdk installation path go build / / compiled language, which needs to be compiled and then executed into an executable file, execute the executable file go run / / compile and execute For use in the development phase, two steps and one step: go get / / download and install the package and rely on the equivalent of pip installgo versiongo fmt / / running gofmt for formatting (gofmt: automatically formatting the code) 3, Hello Word// single-line comments / * multiple-line comments / / Go (all compiled languages) project to run There must be an entry / / Go. The entry is whether there can be multiple main functions under the main function / / main package under the main package. The package name is main. Each Go file belongs to a package import "fmt" / / import package. The built-in package func main () {/ / defines a main function. The curly braces wrap is the content of the function body fmt.Println ("Hello World") / / print function is equivalent to print ()} / / compile go build s1.go// execute s1.exe// compilation and execute go run s1.go// in goland, right-click, run it, variable
The variable definition of go language is mainly divided into three ways, and the definition can not be repeated.
1. Definition method
Method 1: full definition
/ / var variable name variable type = variable value package main import "fmt" func main () {var age int = 10 / / in go, a variable must be used if it is defined, and an error fmt.Println (age)} is reported if it is not used
Method 2: type derivation (type does not need to be written)
Package mainimport "fmt" func main () {var age = 20 var name = "XiaoYang" fmt.Println (age, name) fmt.Printf ("% T\ n", name) / / View variable type\ nindicates newline fmt.Printf ("% p", & name) / / View variable memory address} / / output 20 XiaoYangstring0xc000056230
Method 3: brief declaration (type and var keyword are not written)
Package mainimport "fmt" func main () {age: = 20 var age int = 30 / / repeated definition will result in error, fmt.Println (age)} cannot be repeatedly defined.
Other ways of definition: other ways of definition appear on the basis of the first three ways:
Only define and not assign values:
Var age int / / defines variables. If you only define variables and do not assign values, you can only var age / / errors in this way, and you cannot determine the type.
Declare multiple variables:
Var width, height int = 100,50 / / declare multiple variables var width, height = 100,50 / / declare multiple variables var width, height = 100, "XiaoYang" / / declare multiple variables width, height: = 100,50 / / declare multiple variables
Declare multiple variables and assign initial values:
Var (name = "XiaoYang" age int = 20 height int) fmt.Println (name, age, height)
Note:
Var age int = 20name, age: = "XiaoYang", 21 / / this situation will not report an error, we think it is a duplicate definition, as long as there is an undefined variable on the left side of the colon. Fmt.Println (name, age)
Summary:
The variable type is determined at the definition stage, and once determined, it is not allowed to change.
Variables cannot be defined repeatedly
Variables should be defined before using the
Variable definition specification
Try to use humps in variable naming (case has special meaning)
It is recommended to use an underscore to name Go files.
A name must begin with a letter (Unicode) or an underscore, followed by any letter, number or underscore
Uppercase and lowercase letters are different: Name and name are two different variables
Keywords and reserved words are not recommended as variable names
Third, constant
Constant definition is defined using the const keyword and cannot be changed.
Method 1:
Package mainimport "fmt" func main () {const age int = 20 / / does not allow modification, modification will report an error fmt.Println (age)}
Method 2: type derivation
Package mainimport "fmt" func main () {const age = 20 fmt.Println (age)}
Note: can not be defined in this way: =, this is the definition of variables
4. Basic data type 1, digital / signed shaping int: int32 on 32-bit machine and int64int8 on 64-bit machine: indicates that the integer range is: 8 bits, 8 bit is 1byte, and the first bit represents the positive and negative number 0max 1 So it is the range of 2 to the 7th power-1 int16: 2 to the 15th power minus one int32: 2 to the 32th power minus one int64:2 to the 64th power minus one byte: equal to int8short: equal to int16// unsigned shaping uint8: no negative representation, so it is 2 to the eighth power minus one uint16: ditto uint32:uint64:2, floating point (decimal) Float32float643, Bool / / data type default value: the number type is 0 the string type is the empty string Boolean type false4, the string / / a double quotation mark package / / single quotation mark wraps up here, on "how to understand the Go language variable and the basic data type" the study is over, hope to be able to solve everyone's doubt. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.