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

How to transform Interface objects in golang

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to transform the interface object in golang. It is very detailed and has a certain reference value. Friends who are interested must finish it!

There are two ways to transform interface objects:

1. Method 1: instance,ok:= interface object. (actual type)

If the interface object is the corresponding actual type, then instance is the transformed object, and the value of ok is true.

Cooperate with if...else if... Use

two。 Method 2:

Interface object. (type)

Use with switch...case statement

Example:

Package main import ("fmt"math") type shape interface {perimeter () int area () int} type rectangle struct {an int / / long b int / / wide} func (r rectangle) perimeter () int {return (r.a + r.b) * 2} func (r rectangle) area () int {return r.a * r.b} type circle struct {radios int} func (c circle) perimeter () int { Return 2 * c.radios * int (math.Round (math.Pi))} func (c circle) area () int {return int (math.Pow (float64 (c.radios)) 2) * math.Pi)} func getType (s shape) {if I, ok: = s. (rectangle) Ok {fmt.Printf ("length of rectangle:% d, width of rectangle:% d\ n", i.a, i.b)} else if i, ok: = s. (circle) Ok {fmt.Printf ("radius of the circle is:% d\ n", i.radios)}} func getType2 (s shape) {switch I: = s. (type) {case rectangle: fmt.Printf ("the length of the rectangle:% d, the width of the rectangle is:% d\ n", i.a, i.b) case circle: fmt.Printf ("the radius of the circle is:% d\ n" I.radios)} func getResult (s shape) {fmt.Printf ("the perimeter of the graph is:% d, the area of the graph is:% d\ n", s.perimeter (), s.area ())} func main () {r: = rectangle {a: 10, b: 20} getType (r) getResult (r) c: = circle {radios: 5} getType2 (c) getResult (c)}

The above example uses mode 1. If you want to use mode 2, you can change the getType () function to:

Func getType (s shape) {switch I: = s. (type) {case rectangle: fmt.Printf ("length of the graph:% .2f, width of the graph:% .2f\ n", i.a, i.b) case triangle: fmt.Printf ("the first edge of the graph:% .2f, the second edge of the graph:% .2f The third edge of the graph:% .2f\ n ", i.arect i.bscoii.c) case circular: fmt.Printf (" Radius of the graph:% .2f\ n ", i.radius)}} above is all the content of this article" how to transform interface objects in golang ". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow 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.

Share To

Development

Wechat

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

12
Report