In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to write example code for Go language implementation enumeration. The content is concise and easy to understand. It will definitely make your eyes shine. I hope you can gain something through the detailed introduction of this article.
In programming, enumerations are used to represent types that contain only a limited number of fixed values, and in development they are commonly used to identify error codes or state machines. For example, the state machine of an entity object usually corresponds to the field value that identifies the state of the corresponding record of the object in the database.
When you first started programming, you must have written, or at least seen, code that used magic numbers to make judgments. What is magic number? For example, when you want to top an article, you first judge whether the article has been published.
if (article.state == 2) { // state 2 means the article has been published}
Would it bother you if we didn't have comments in our code, or if our project code was filled with magic number judgments?
Later, I learned to define these state values as constants, and also developed a method to determine the state of the object to encapsulate this logic separately.
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 is definitely much stronger than using magic numbers to judge directly in the program. At least it won't be a headache and won't want to curse.
However, later my big brother said that this also has shortcomings, the above checkArticleState method is used to check the article state, the intention is to let the caller pass one of the three static constants of ArticleState, but because there is no type constraint, so passing any int value is syntactically allowed, the compiler will not raise any warnings, and enumeration is more appropriate.
em~! I don't remember the semester when I taught Java in college. The teacher talked about this stuff. Could it be another knowledge point missed by playing mobile phones in class? So using enumerations our Java code becomes:
//declare public enum ArticleState { //create all enumeration objects in enum Draft(1, Draft); Published(2, "Published"); Deleted(3, "Deleted") //Custom Properties private int code; private String text; //constructor must be private ArticleState(int code, String text) { this.code = id; this.text = name; }}public Boolean checkArticleState(ArticleState state) { ... }
In this way, we can filter out illegal state values by relying on the enumeration type of formal parameters. When passing integer values as parameters to checkArticleState method, because the type does not match, the compiler can immediately prompt it when writing code.
If you haven't used Java, you don't have to struggle. I marked the main syntax points with comments. Everyone should be able to understand them. In the past two years, I mainly used Go to do projects. I found similar problems in Go, but Go did not provide enumeration types. How to do the correct restriction on state values? If you still use int constants, it definitely won't work. For example:
const ( Draft int = 1 Published = 2 Deleted = 3 ) const ( Summer int = 1 Autumn = 2 Winter = 3 Spring = 4 ) func main() { //output true, there will be no compilation errors fmt.Println(Autumn == Draft) }
For example, two sets of int constants are defined above, one representing the state of the article and the other representing the seasons. This way of comparing article status to seasons does not cause any compilation errors.
The answer can be found in Go's built-in libraries or in code from open source libraries we all know about. For example, look at google.golang.org/grpc/codes gRPC error code is how to define, you can understand how to correctly implement enumeration.
We can use int as a base
type Season intconst ( Summer Season = 1 Autumn = 2 Winter = 3 Spring = 4)
Type creates an alias type, Go supports this
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 do not match, compilation error fmt.Println(Autumn == Draft) //parameter types do not match, but because the underlying type of ArticleState is int, implicit type conversion occurs when int is passed, so no error will be reported. checkArticleState(100) }
Although the underlying types of these state values are int values, comparing enumeration values of two unrelated types now causes compilation errors because we now have type restrictions everywhere 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. Therefore, when calling checkArticleState, passing int type parameters will have implicit type conversion, which will not cause compilation errors. If you want to solve this problem, you can only redefine the type to achieve it.
The above content is how to write the example code of Go language implementation enumeration. Have you learned knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to 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.