Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the types of assertions in golang

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I would like to talk to you about the types of assertions in golang, which many people may not know very well. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something from this article.

1) Grammar one

T: = I (T)

Explanation:

T: represents the specific data type, and I: represents the interface variable iJournal t: represents the converted variable.

Function introduction:

The function of this statement is to convert the interface variable I to t according to the type T, where the value in t is converted by I, and a panic will be triggered if the conversion is not successful.

2) in addition, there is another way to write:

T, ok: = I (T)

Function introduction:

There is an extra ok in the expression, and the function is to convert interface I to t according to type T, if the type matches ok=true, if the type does not match, ok=false. Changing the sentence does not trigger panic.

3) the example is as follows:

Package main

Import "fmt"

Func main () {var i interface {} = "hello world"

Str: = I. (string) / / 1. Interface I is of type string and assigns "hello world" to str fmt.Println (str)

S, ok: = I. (string) / / 2. The interface type is string, which assigns "hello world" to s, and ok=true fmt.Println (s, ok)

Idx, ok: = I. (int) / / 3. If the interface type is not int,ok=false,idx, the value fmt.Println (idx, ok) idx = I. (int) / / 4 will not be obtained from I. Interface I is not of int type and will trigger panic fmt.Println (idx)}

Output:

Hello worldhello world true0 falsepanic: interface conversion: interface {} is string, not int

Goroutine 1 [running]: main.main () / tmp/sandbox277669744/prog.go:17 + 0x1f4

3. Usage scenarios for assertion types:

1) the type of T is a specific type

The type assertion checks whether the dynamic type of x is equal to the concrete type T. If the check is successful, the result returned by the type assertion is the dynamic value of x of type T.

The example can refer to the example in 2, and there is no particular difference.

2) the type of T is the interface type

Type assertions check whether the dynamic type of x satisfies T. If the check is successful, the dynamic value of x is not extracted, and the return value is an interface value of type T. In other words, type assertions to interface types change the type of expression, change (usually expand) accessible methods, and protect dynamic types and values within interface values.

Example:

Package main

Import ("fmt")

Type I interface {walk ()} type J interface {fly ()} type A struct {}

Func (an A) walk () {fmt.Println ("walk!")} func (an A) fly () {fmt.Println ("fly!")} func main () {var i I i = A {} / / dynamic type of i is A fmt.Printf ("% T\ n" I. (a)) / / i.fly () / / error:. / prog.go:26:6: i.fly undefined (type I has no field or method fly) var j J j = I. (J) / / here the value in I is converted to interface J, a type fmt.Printf ("% T\ n", j) j.fly ()}.

Output:

Main.Amain.Afly! After reading the above, do you have any further understanding of the types of assertions in golang? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report