In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to carry out a simple analysis of the Go programming language, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Go is an imperative programming language for concurrent programming. It is mainly developed by the creator Google, initially mainly by Robert Griesemer, Rob Pike, and Ken Thompson. The design of the language began in 2007 and the initial version was released in 2009, while the stable version was version 1.0 released in 2012. one
Go has a C-style syntax (no preprocessor), garbage collection mechanism, and similar to its predecessors developed in Bell Labs: Newsqueak (Rob Pike), Alef (Phil Winterbottom) and Inferno (Pike, Ritchie, etc.), using the so-called Go protocol goroutines and channel channels (a Hoare-based "communication sequential process" theory) to provide built-in concurrency support. two
Go programs are organized as packages. A package is essentially a folder that contains Go files. All files in a package share the same namespace, while symbols in a package have two kinds of visibility: symbols that begin with uppercase letters are visible to other packages, while other symbols are private to the package:
Func PublicFunction () {fmt.Println ("Hello world")} func privateFunction () {fmt.Println ("Hello package")} type
Go has a fairly simple type system: no subtypes (but there are type conversions), no generics, no polymorphic functions, only some basic types:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
Basic types: int, int64, int8, uint, float32, float64, etc.
Struct
Interface: a collection of methods
Map [K, V]: a mapping from key types to value types
[number] Type: an array of elements of type Type
[] Type: some type of slice (pointer to an array of length and function)
Chan Type: a thread-safe queue
Pointer * T points to other types
Function
Named type: aliases of other types that may have associated methods. (LCTT translation note: aliases here do not refer to the new feature "type aliases" in Go 1.9):
Named types are completely different from their underlying types, so you can't assign them to each other, but some operators, such as +, can handle named type objects of the same underlying numerical type (so you can add up the two Ts in the example above).
Type T struct {foo int}
Type T * T
Type T OtherNamedType
Mappings, slices, and channels are types similar to references-- they are actually structures that contain pointers. Other types, including arrays (which have a fixed length and can be copied), are value passing (copying).
Type conversion
Type conversions are similar to type conversions in C or other languages. They are written like this:
TypeName (value) constant
Go has "untyped" literals and constants.
1 / / untyped integer face const foo = 1 / / untyped integer constant const foo int = 1 / / int type constant
Untyped values can be divided into the following categories: UntypedBool, UntypedInt, UntypedRune, UntypedFloat, UntypedComplex, UntypedString, and UntypedNil (Go calls them base types, and other base types can be used for specific types, such as uint8). An untyped value can be assigned to a named type derived from the underlying type; for example:
Type someType intconst untyped = 2 / / UntypedIntconst bar someType = untyped / / OK: untyped can be assigned to someTypeconst typed int = 2 / / intconst bar2 someType = typed / / error: int cannot be assigned to someType interfaces and objects
As mentioned above, an interface is a collection of methods. Go itself is not an object-oriented language, but it supports associating methods to named types: when you declare a function, you can provide a receiver. The receiver is an extra argument to the function that can be passed before the function and participate in the function lookup, like this:
Type SomeType struct {...} type SomeType struct {...} func (s * SomeType) MyMethod () {} func main () {var s SomeType s.MyMethod ()}
If the object implements all the methods, it implements the interface; for example, * SomeType (note pointer) implements the following interface MyMethoder, so the value of type * SomeType can be used as the value of type MyMethoder. The most basic interface type is interface {}, which is an interface with an empty set of methods-- any object satisfies that interface.
Type MyMethoder interface {MyMethod ()}
There are some restrictions on legal recipient types; for example, a named type can be a pointer type (for example, type MyIntPointer * int), but this type is not a legal recipient type.
Control flow
Go provides three main control statements: if, switch, and for. These statements are very similar to statements in other C-style languages, but with some differences:
Conditional statements have no parentheses, so the conditional statement is if a = = b {} instead of if (a = = b) {}. Curly braces are necessary.
All statements can be initialized, such as this if result, err: = someFunction (); err = = nil {/ / use result}
The switch statement can use any expression in the branch
Switch statements can handle empty expressions (equal to true)
By default, Go does not move from one branch to the next (no break statement is required), and using fallthrough at the end of the block goes to the next branch.
The loop statement for can not only loop the value range: for key, val: = range map {do something}
Go cooperation program
The keyword go generates a new Go protocol goroutine, which is a function that can be executed in parallel. It can be used for any function call, even an anonymous function:
Func main () {... Go func () {...} () go some_function (some_argument)} channel
Go protocols are usually combined with channel channels to provide an extension of communication sequential processes. The channel is a concurrency secure queue, and you can choose whether or not to buffer data:
Var unbuffered = make (chan int) / / complete block sending var buffered = make (chan int, 5) / up to 5 unread blocks when the data is read
Operator
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.