In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what is the meaning of Python's isinstance in Golang". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
We know that in Python, to determine whether a variable is of a certain type, you only need to use the keyword isinstance:
Def calc (param): if isinstance (param, int): return param + 1 elif isinstance (param, str): return None
Golang is a static language, although in most cases, the type of a variable is determined when it is defined, and there is no such thing as an integer and a string. But Golang has a type called interface, which can store any type of data. Let's look at an example:
Package main import ("fmt") func main () {var param interface {} param = 123fmt.Println ("at first, param is:", param) param = "xyz" fmt.Println ("later, param is:", param)}
The running effect is shown in the following figure:
In general, we really don't take the initiative to get ourselves into trouble, deliberately defining an interface type that can be used everywhere. But sometimes it has to be done. For example, to create a map whose values may have multiple types, you must use interface.
So, what if one of our functions takes an argument of interface and needs to implement different logic depending on the type of data stored in it? There are several ways to do this in Golang.
Print type only
If you only need to print out the data types in interface, you can use% T to do this:
Package main import ("fmt") func main () {var param interface {} param = 123fmt.Printf ("data type:% T\ n", param)}
The running effect is shown in the following figure:
If you need to store this type in another variable, it is also easy:
Package main import ("fmt") func main () {var param interface {} param = 123param_type: = fmt.Sprintf ("% T", param) if param_type = = "int" {/ / Note that the types here are all fmt.Println in string form ("confirm to be an integer!") }}
The running effect is shown in the following figure:
Multiple judgment
If there may be many types of data in interface, each with a different processing scheme, you can use the switch type syntax:
Package main import ("fmt") func main () {var param interface {} param = 123 switch param. (type) {case int: / / Note that the types here are the fmt.Println of the type itself ("data is an integer!") Case string: fmt.Println ("data is a string!") Default: fmt.Println ("other types!") }}
The running effect is shown in the following figure:
It should be noted, however, that param. (type) this syntax can only be used in switch. Used in type, if used alone, an error will be reported.
Just judge whether it is a certain type or not.
If I only need to determine whether the data in interface is of a particular type, there is an easier way to do this:
Package main import ("fmt") func main () {var param interface {} param = 123, yes: = param. (float64) if yes {fmt.Println ("is float64 type")} else {fmt.Println ("not float64 type")}}
The running effect is shown in the following figure:
Among them, param. (specific type) two data are returned. The first data is the data in the interface, and the second data is of bool type. If the data in the interface is of a specific type, true is returned, otherwise false is returned.
This is the end of the content of "what is the meaning of Python's isinstance in Golang". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.