In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail the grammatical sugar that should be paid attention to and what is the difference between go and java C++. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Background
Recently, I have been using golang to write k8s operator (which involves informer controler) for the internal scheduling platform, which happens to be able to compare with the java version of informer controller. As I have not had much contact with golang before, I have encountered a different syntax sugar from java C++. Now list:
Specific package in different glang
Each Go file belongs to and only belongs to one package. You must indicate which package the file belongs to in the first non-commented line of the source file, for example, package main,package main represents a program that can be executed independently.
Each Go application contains a package called main. There can be multiple files under the package main package, but there can only be one main () method in all files, and the main () method represents the program entry.
Struct in golang
There is no concept of class in golang, but there is struct, and you can add methods to struct as follows:
Type Member struct {Id int `json: "id" `} / / binds to the method of the Member structure, but this does not change the value of the member, because the structure is value passing, and when we call setID, the method receiver receives a copy of the structure variable, and fixes the value through the copy without affecting the caller, so we can define the method receiver as a pointer variable. You can modify the structure func (m Member) setId (id int) {m.Id = id} m: = Member {} m.setId (1) fmt.Println (m.Id) / / the output is empty / / bound to the Member structure, and the member value func (m * Member) setId (id int) {m.Id = id} m: = Member {} m.setId (1) fmt.Println (m.Id) / / output to 1
For more information about how the recipient of the method and the interface are called, refer to the following:
Regardless of whether the receiver of the method is a value or a pointer, the value and pointer of the object can call the method type, so the recipient of the method can be either a value type or a pointer type
When the receiver of a method is a value, whether it is a value call or a pointer call, the method operates on a copy of the original object without affecting the original object type, so the receiver of the method must also be a value type before the method can be called.
When the receiver of a method is a pointer, whether it is a value call or a pointer call, the method operates on the original object through the pointer, which will affect the original object.
Note: the property of struct can have no name but only the type, and the type is the property name when used. (therefore, there can be only one anonymous property of the same type in a struct)
Define the tags of struct
When defining the structure of strut, you can add that tag,tag can be used at run time and when forming a json or xml as follows:
Type NetworkList struct {Project `json: ", inline" `f2 int `json: "id,-" `f3 string` json: "f3 string omitempty" `f4 string `json: "f4" `} type Project struct {Key string `json: "key" `Value string `json: "value"`}
Among them
Noun | explain-|-json: ", inline" | indicates that the embedded key and the key of the outer struct are parallel. For example, key and Project key in NetworkList are parallel when forming json, not embedded, that is, at the same level json: "id,-" |-(horizontal bar) indicates a private field, and the key json is not included when the json is formed: "f3memomitempty | omitempty indicates that the field is empty, then json is generated. Does not include the key json: "f4" | indicates that when json is generated, key is f4
Type conversion of golang
There are implicit type conversions in C++ java, not in golang, and cast and type assertions in golang.
Forced type conversion
Package mainimport "fmt" func main () {var a float32 = 5.6var b int = 10 fmt.Println (a * float32 (b))}
Type assertion
Package mainimport "fmt" func main () {var an interface {} = 10t if ok oklu = a. (int) if ok {fmt.Println ("int", t)} T2 if ok fmt.Println ("float32", T2)} golang in interface {}
Interface {} is a null interface without any methods, and all types implement null interfaces, which is equivalent to a pointer to the object,interface type in java by default
Go keyword in golang
The go keyword is used to create goroutine, which is the key to concurrency.
/ / the go keyword is placed before the method call to create a new goroutine and let it execute the method body go GetThingDone (param1, param2); / / A variant of the above example, create a new anonymous method and execute go func (param1, param2) {} (val1, val2) / / directly create a new goroutine and execute the defer keyword in the code block go {/ / do someting...} golang in goroutine
Defer executes in a manner similar to destructors in other languages, executing one by one in the reverse order of call order after the end of function body execution.
It executes even if a serious error occurs in the function, which is equivalent to finally
Commonly used for resource cleanup, file closing, unlocking, recording time and other operations
Func main () {fmt.Println ("a") defer fmt.Println ("b") defer fmt.Println ("c")} / print / / init function in a//c//bgolang
The init function of golang is executed only when the file is referenced (when it is import, not when the package function is called)
Package libimport "fmt" func init () {fmt.Println ("lib empty init")}-package mainimport ("Test/lib"fmt") func main () {fmt.Println ("wint")} / output / / lib empty init / / wint anonymous function
This usage is similar to that in java C++.
Func () {/ / func body} () / / curly braces followed by () indicates a function call. It is declared here as a specified parameter list, / / so there is no need to pass parameters to share here about the syntax sugars that should be noted in go and java C++ and what are the differences. I hope the above can be helpful and learn more. If you think the article is good, you can share it for more people to see.
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.