Get the App
SLTechnology News&Howtos  ›  Development  › 

The method of query Type assertion in GO language

Shulou Source: shulou.com Published: 2022-05-31 22:14:42 10月03日 Update

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.

Tags: Type content query method variable language v.data knowledge subscript function character string actual numeric floating point industry storage test different Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB OPPO Reno macOS Shulou Technology Microsoft