There are two ways to transform interface objects in golang

Time:2022-5-5

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 object after transformation, and the value of OK is true
If else if… use

2. Mode 2:

Interface object (type)
With switch Case statement use

Example:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
 
import (
    "fmt"
    "math"
)
 
type shape interface {
    perimeter() int
    area() int
}
 
type rectangle struct {
    A int // long
    B int // width
}
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.Round(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 circle is:% d \ n", i.radios)
    }
}
 
func getType2(s shape) {
    switch i := s.(type) {
    case rectangle:
        fmt. Printf ("length of rectangle:% D, width of rectangle:% d \ n", I.A, I.B)
    case circle:
        fmt. Printf ("radius of 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:

?
1
2
3
4
5
6
7
8
9
10
func getType(s shape) {
 switch i := s.(type) {
 case rectangle:
  fmt. Printf ("length of graph:%. 2F, width of graph:%. 2F \ n", I.A, I.B)
 case triangle:
  fmt. Printf ("first edge of graph:%. 2F, second edge of graph:%. 2F, third edge of graph:%. 2F \ n", I.A, I.B, I.C)
 case circular:
  fmt. Printf ("radius of graph:%. 2F \ n", i.radius)
 }
}

PS: Helen’s formula is used to calculate the area of the triangle. The formula is:

Area of triangle = square root [half of circumference of triangle] × (half the circumference of the triangle minus the first side) × (half the circumference of the triangle minus the second side) × (half the circumference of the triangle minus the third side)]

This is the end of this article about the transformation of interface objects in golang. For more information about golang interface objects, please search the previous articles of developeppaper or continue to browse the relevant articles below. I hope you will support developeppaper in the future!