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 > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to get started with Golang development, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
1 process control 1.1 switch control flow 1.1.1 basic syntax
Switch expression {
Case expression 1, expression 2:
Statement block 1
Case expression 3, expression 4:
Statement Block 2
Default:
Sentence block
}
1.1.2 pay attention to details
1) multiple expressions can be matched after the case of switch in Golang, separated by commas
2) after the case statement block in Golang, there is no need for break to show the pop-out. The program will automatically jump out of the switch after executing the statement block after matching.
3) after case, there is an expression (i.e., constant, variable, a function with a return value, etc.)
4) if the expression after case is constant, it must not be repeated.
5) the data type of the value of each expression after case must be the same as that of switch.
6) switch can be followed without expressions, similar to if-else branches, such as
7) after switch, you can also declare / define a variable directly. The semicolon ends and is not recommended.
8) switch traversal-fallthrough. If fallthrough is added after the case statement block, the next case will be executed, which is also called switch traversal.
9) the Type switch:switch statement can also be used in Type-switch to determine the type of variable actually pointed to in an interface variable, as follows
1.2 for Loop 1.2.1 pay attention to details
1) several writing methods of for cycle
Func main () {for I: = 0; I main function
2.5 Anonymous function
Go supports hidden functions. If a function wants to be called only once, consider using hidden functions.
2.5.1 how to use anonymous functions
1) method 1: call directly when defining a hidden function, such as
2) Mode 2: assign the hidden function to a variable (function variable), and then call the hidden function through that variable.
3) Global hidden function
Assign a hidden function to a global variable, then the hidden function becomes a global hidden function.
2.6 closure
Simply understand that a closure is a function that can read internal variables of other functions, that is, functions defined within a function, such as
2.7 use of defer
Resources (database connections, file handles, locks, etc.) often need to be created in a project. In order to release resources in time after the execution of the function, the designers of go provide defer (delay mechanism).
Example 1
Example 2, close the file resource
Func main () {/ / close file resources file = openfile (file name) defer file.close () / / other statements} 2.7.1 Note details
When you put the defer statement on the stack, you will also put a copy of the relevant value on the stack, as follows
2.8 the way in which function parameters are passed
1) value transfer
2) reference transfer
In fact, whether it is value transfer or reference transfer, what is passed to the function is a copy of the variable, the difference is that value transfer is a copy of the value, reference transfer is a copy of the address, generally speaking, the copy of the address is efficient, because the amount of data is small, and the value copy determines the size of the data of the value, the larger the data, the lower the efficiency.
2.8.1 string function
1) count the length of the string, using the built-in function len (str) by byte
2) string traversal while dealing with problems including Chinese r: = [] rune (str)
3) string to integer, using strconv.Atoi function
4) convert integer to string, using strconv.Itoa function
5) string to [] byte, such as var b = [] byte ("hello")
6) [] byte is converted to a string, such as str: = string ([] byte {97 and 98))
7) use strconv.FormatInt to convert decimal to 2, 8, and hexadecimal strings
8) to determine whether a string contains substrings, use strings.Contains
9) there are several non-repeating substrings in the returned string, such as
10) determine whether the string is equal
2.9 date function
1) format date. Use now.Format to format date. The date must be 15:04:05 on 2006-01-02.
2) format the date, using the fmt.Sprintf function to format and return a string to implement
2.10 built-in function
1) new: used to allocate memory, mainly used to allocate value types, such as int, float32. The pointer is returned.
2) make: used to allocate memory, mainly to allocate reference types, such as channel, map.
2.11 exception handling mechanism
The go language pursues simplicity and elegance, so the traditional try...catch...finally handling is not supported in the GE language, but the exception is handled through defer,panic,recover, which can be understood as throwing an exception of panic in go, then catching the exception through recover in defer, and then handling it accordingly.
Func test () {/ / use defer + recover to catch and handle exceptions defer func () {/ / use recover to catch exceptions err: = recover () if err! = nil {fmt.Println ("error, cause is:", err)}} () I: = 10 j: = 0 fmt.Println (iUnip j)} func main () {test () fmt.Println ("Test")} 2.12 Custom error
Custom errors are also supported in Go programs, using errors.New and panic built-in functions
1) errors.New ("error message"), which returns a value of type error, indicating an error
2) panic built-in function, which receives a value of type interface {} as a parameter, receives a variable of type error, outputs an error message and exits.
The above content is how to get started with Golang development. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.