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 focuses on "the basic usage of Golang enumerated types". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn the basic usage of Golang enumerated types.
Basic work
For the convenience of the following explanation, go modules is used here to create a simple project.
~ / Projects/go/examples ➜mkdir enum ~ / Projects/go/examples➜ cd enum ~ / Projects/go/examples/enum ➜go mod init enum go: creating new go.mod: module enum ~ / Projects/go/examples/enum➜ touch enum.go
Const + iota
Take the start, run, and stop states as an example, using the const key to declare a series of constant values. Write the following in enum.go:
Package main import "fmt" const (Running int = iota Pending Stopped) func main () {fmt.Println ("State running:", Running) fmt.Println ("State pending:", Pending) fmt.Println ("State Stoped:", Stopped)}
Save and run, you can get the following results
~ / Projects/go/examples/enum ➜go run enum.go State running: 0 State pending: 1 State Stoped: 2
Before we explain what happened, let's take a look at one thing, iota. In contrast to the constant counter provided in c and java,go, iota uses continuous assignment of constants when declaring constants.
For example, this example
Const (an int = iota / / a = 0b int = iota / / b = 1 c int = iota / / c = 2) const d int = iota / / d = 0
In a const declaration block, the initial value of iota is 0, incrementing by 1 for each variable declared. The above code can be simplified to:
Const (an int = iota / / a = 0 b / / b = 1 c / / c = 2) const d int = iota / / d = 0
Imagine what it would be like to write it in c and java if there were 50 or 100 constants at this time.
There are more specific techniques for iota (such as hop count), see the official definition of iota for details.
By using const to define a series of constants, and with the help of iota constant counter, it is very convenient to quickly assign continuous values to constants of numerical types. Although there is no enum keyword, in this case, it is found to be redundant, and enumerations are essentially a combination of constants.
Of course, you can use the following ways to get closer to enum in other languages
/ / enum.go... Type State int const (Running State = iota Pending Stopped)...
Is it more like enum {} in other languages to wrap a set of constant values with a type alias?
You can also change the above example to:
/ / enum.go... Type State int const (Running State = iota Pending Stopped) func (s State) String () string {switch s {case Running: return "Running" case Pending: return "Pending" case Stopped: return "Stopped" default: return "Unknown"}}.
Add the String function to the defined enumeration type, and the result is as follows:
~ / Projects/go/examples/enum ➜go run enum.go State running: Running State pending: Pending State Stoped: Stopped
At this point, I believe you have a deeper understanding of the "basic usage of Golang enumerated types". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.