In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how to achieve enumeration in Golang, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value, I believe you will have something to gain after reading this Golang article on how to achieve enumeration, let's take a look.
In the field of programming, enumerations are used to represent types that contain only a limited number of fixed values, and are generally used to identify error codes or state machines in development. Take the state machine of an entity object, which usually corresponds to the field value of the corresponding record of the object in the database.
If (article.state = = 2) {/ / state 2 represents the article has been published}
Will it be a headache if there are no comments in our code, or when the code of our project is full of judgments about these magic numbers?
Later, I learned to define these state values as constants, and I also developed a method to determine the state of the object to encapsulate this logic.
Public class ArticleState {public static final int Draft = 1; / / draft public static final int Published = 2; / / publish public static final int Deleted = 3; / / deleted} public Boolean checkArticleState (int state) {.}
This usage must be much better than directly using magic numbers in the program, at least it doesn't look like a headache and doesn't want to scold *.
However, later, the eldest brother who took me at that time said that this also had its drawbacks. The above checkArticleState method was used to check the status of the article. The original intention is to let the caller pass one of the three static constants of ArticleState, but since there is no type constraint, it is also allowed to pass any int value in syntax, and the compiler will not give any warning, so enumeration is more appropriate.
Emblem! I don't remember the teacher talking about this in the semester when I was teaching Java in college. Is it another knowledge point that I missed playing with my cell phone in class? So after using enumeration, our Java code becomes:
/ use enum instead of class to declare public enum ArticleState {/ / to create all enumerated objects in enum Draft (1, "draft"); Published (2, "released"); Deleted (3, "deleted") / / Custom property private int code; private String text / / the constructor must be private's ArticleState (int code, String text) {this.code = id; this.text = name;}} public Boolean checkArticleState (ArticleState state) {...}
In this way, we can rely on the enumerated type of the parameter to help us filter out illegal state values. When passing the integer value as a parameter to the checkArticleState method, it can not be compiled because of type mismatch, and the compiler can prompt it immediately when writing code.
If you haven't used Java, you don't have to worry about it. I've annotated the main grammar points, and everyone should be able to understand them.
In the past two years, I mainly used Go to do projects, and I found similar problems in Go, but Go did not provide enumerated types, so how to make the correct restrictions on state values? It certainly won't work if you still use the constant of int type. For example:
Const (Draft int = 1 Published = 2 Deleted = 3) const (Summer int = 1 Autumn = 2 Winter = 3 Spring = 4) func main () {/ / output true without any compilation errors fmt.Println (Autumn = = Draft)}
For example, two sets of int type constants are defined above, one representing the status of the article and the other representing the seasons. There will be no compilation errors in this way to compare the status of the article with the season.
The answer can be found in the code of the Go built-in library or some of the open source libraries we all know. For example, if you look at how the error codes of gRPC in google.golang.org/grpc/codes are defined, you can see how to implement enumerations correctly.
We can use int as the base type to create an alias type, which is supported in Go
Type Season intconst (Summer Season = 1 Autumn = 2 Winter = 3 Spring = 4)
Of course, iota is often used in Go when defining continuous constant values, so the above definition can be further simplified.
Type Season intconst (Summer Season = iota + 1 Autumn Winter Spring) type ArticleState intconst (Draft int = iota + 1 Published Deleted) func checkArticleState (state ArticleState) bool {/...} func main () {/ / two Operand types mismatch, compilation error fmt.Println (Autumn = = Draft) / / parameter type mismatch But because the underlying type of ArticleState is int, implicit type conversion occurs when int is passed, so there is no error checkArticleState (100)}.
Although the underlying types of these state values are int values, comparing enumerated values of two unrelated types now results in compilation errors because we now have type restrictions on where we use state values.
However, the parameter type of the function checkArticleState is set to ArticleState, but because the underlying type of ArticleState is int. So passing parameters of int type when calling checkArticleState will cause implicit type conversion, which will not cause compilation error. If you want to solve this problem, you have to redefine the type to implement it.
This is the end of the article on "how to implement enumeration in Golang". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to achieve enumeration in Golang". If you want to learn more, 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.