In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Features:
a. Automatic garbage collection
b. Rich built-in types
c. Multiple return values of function
d. Error handling
e. Anonymous functions and closures
f. Type and interface
G.goroutine concurrent programming
h. Reflection
I.CGO calls c function
The go language starts with the main function in the main package, and the Go language main () function cannot take parameters or define a return value. The parameters passed in on the command line are saved in the os.Args variable. If you need to support command line switches, you can use the flag package. After the package declaration, a series of import statements are used to import the package that the program depends on.
Package main
Import "fmt" / / We need to use the Println () function in the fmt package
Func main () {
Fmt.Println ("Hello, world. Hello, World!")
}
"go run hello.go
"go build hello.go
1. Variable
= assignment operator
: = type inference with initial value attached
a. Declaration of various variables
Var v1 int
Var v2 string
Var v3 [10] int / / array
Var v4 [] int / / Array slice
Var v5 struct {
F int
}
Var V6 * int / / pointer
Var v7 map [string] int / / map,key is string type, value is int type
Var v8 func (an int) int
Const zero = 0.0
b. Class when a variable is declared
Var v1 int = 10 / / correct usage 1
Var v2 = 10 / / correct usage 2, the compiler can automatically deduce the type of v2
V3: = 10 / / the correct use mode 3, the compiler can automatically deduce the type of v3
Go is a strongly typed language. You can specify the data type when defining a variable, or you can choose to derive it automatically.
c. Variable assignment
I, j = j, I / / supports multiple assignments
d. The anonymous variable "" represents
Suppose the GetName () function is defined as follows, and it returns three values, namely firstName, lastName, and
NickName:
Func GetName () (firstName, lastName, nickName string) {
Return "May", "Chan", "Chibi Maruko"
}
If you only want to get the nickName, the function call statement can be written as follows:
NickName: = GetName () assume that the definition of the GetName () function is as follows, which returns three values, firstName, lastName, and
NickName:
Func GetName () (firstName, lastName, nickName string) {
Return "May", "Chan", "Chibi Maruko"
}
If you only want to get the nickName, the function call statement can be written as follows:
, _, nickName: = GetName ()
e. Constant, adding const when defining
Const Pi float64 = 3.14159265358979323846
Const zero = 0.0 / / untyped floating point constant
Const (
Size int64 = 1024
Eof =-1 / / untyped integer constant
)
Const u, v float32 = 0,3 / / u = 0.0,v = 3.0,multiple assignment of constant
Const a, b, c = 3,4, "foo"
/ a = 3, b = 4, c = "foo", untyped integers and string constants
f. Enumerate
The following is a general enumeration representation that defines a series of integer constants:
Const (
Sunday = iota
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
NumberOfDays / / this constant is not exported
)
Like other symbols in the Go language (symbol), constants that begin with uppercase letters are visible outside the package.
In the above example, numberOfDays is private within the package, and other symbols can be accessed by other packages.
Data types of g.go language
Boolean type: bool.
integers: int8, uint8, byte, int16, int, uint, int32, int64, uintptr, etc.
floating point type: float32, float64.
plural type: complex64 (plural type consisting of 2 float32), complex128. 2i 3i
string: string.
character type: rune. Represents a single Unicode character.
error type: error.
In addition, the Go language supports the following composite types:
pointer (pointer)
array (array)
slice (slice)
Dictionary (map)
Channel (chan)
structure (struct)
Interface (interface)
String value and its traversal
Str: = "Hello, World"
N: = len (str)
For I: = 0; I < n; iTunes + {
Ch: = str [I] / / str [I] = "x"; error, the string cannot be changed again, the array is fine
Fmt.Println (I, ch)
}
The use of arrays
[32] an array of 32 byte / / length, with one byte for each element
[2N] struct {x, y int32} / / complex type array
[1000] float64 / / pointer array
[3] [5] int / / 2D array
[2] [2] [2] float64 / / is equivalent to 2
Var myArray [10] int = [10] int {1,2,3,4,5,6,7,8,9,10}
MySlice: = myArray [:] / / create array slices based on all elements of myArray
MySlice: = myArray [: 5] / / create array slices based on the first five elements of myArray
MySlice: = myArray [5:] / / create an array slice based on all elements starting with the fifth element
MySlice: = make ([] int, 5) / / create an array slice with an initial number of 5 elements with an initial value of 0:
MySlice: = make ([] int, 5,10) / / create an array slice with an initial number of elements of 5, with an initial value of 0, and reserve storage space for 10 elements: len (mySlice) = 5
Cap (mySlice) = 10, mySlice = append (mySlice, 1,2,3), any array can grow dynamically
MySlice: = [] int {1, 2, 3, 4, 5} / / directly create and initialize array slices containing 5 elements:
For _, v: = range myArray {/ / array traversal
Fmt.Print (v, "")
}
For I: = 0; I
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.