In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "the method of query type assertion in GO language". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "the method of query type assertion in GO language" can help you solve the problem.
Type query
We know that interface can store any type of value in its variables (this type implements interface). So how do we know in reverse which type of object is actually stored in this variable? There are two commonly used methods at present:
Comma-ok assertion
Switch test
1.comma-ok assertion
There is a syntax in the Go language that can directly determine whether it is a variable of this type: value, ok = element. (T), where value is the value of the variable, ok is a bool type, element is the interface variable, and T is the assertion type.
If the element does store a value of type T, ok returns true, otherwise it returns false.
Var I [] interface {} I = append (I, 10,3.14, "aaa", demo15) for _, v: = range I {if data, ok: = v. (int); ok {fmt.Println ("integer data:", data)} else if data, ok: = v. (float64); ok {fmt.Println ("floating point data:", data)} else if data, ok: = v. (string) Ok {fmt.Println ("string data:", data)} else if data, ok: = v. (func ()) Ok {/ / function call data ()} 2. Switch test var I [] interface {} I = append (I, 10,3.14, "aaa", demo15) for _, data: = range I {switch value:=data. (type) {case int: fmt.Println ("integer", value) case float64: fmt.Println ("floating point") Value) case string: fmt.Println ("string", value) case func (): fmt.Println ("function", value)}} type assertion
If judgment
Package mainimport "fmt" type Student struct {name string id int} func main () {I: = make ([] interface {}, 3) I [0] = 1 / / int I [1] = "hello go" / / string I [2] = Student {"mike", 666} / / Student / / Type query Type assertion / / the first returns the subscript, the second returns the value corresponding to the subscript, data is I [0], I [1], I [2] for index, data: = range I {/ / the first returns the value, and the second returns the true or false if value of the judgment result, ok: = data. (int) Ok = = true {fmt.Printf ("x [% d] type is int, content is% d\ n", index, value)} else if value, ok: = data. (string) Ok = = true {fmt.Printf ("x [% d] type is string, index, value)} else if value, ok: = data. (Student) Ok = = true {fmt.Printf ("x [% d] type is Student, content is name =% s, id =% d\ n", index, value.name, value.id)}
Switch judgment
Package mainimport "fmt" type Student struct {name string id int} func main () {I: = make ([] interface {}, 3) I [0] = 1 / / int I [1] = "hello go" / / string I [2] = Student {"mike", 666} / / Student / / Type query Type assertion for index, data: = range I {switch value: = data. (type) {case int: fmt.Printf ("x [% d] type is int, content is% d\ n", index, value) case string: fmt.Printf ("x [% d] type is string Content is% s\ n ", index, value) case Student: fmt.Printf (" x [% d] type is Student, content is name =% s, id =% d\ n ", index, value.name, value.id)} the content of" methods for GO language query type assertions "ends here. Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.