Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money

Let’s review the interface{} we talked about last time.

  • It can be used to do polymorphism

  • Interface types are divided into empty and non-empty interface types, which have different underlying data structures

Here, by the way, the conditions for posturing are as follows:

  • First you have to have a superclass pointer to a subclass object
  • The interface must also be non-null and contain methods, that is, the underlying data structure used is iFace
  • A subclass implements the concrete methods of its parent class

Interface {} pay attention to assertions

Use assertions normally and write a simple assertion demo

type Animal interface{}

func main(a) {
	var a Animal = "xiaomotong"
	v, ok := a.(string)
	if! ok{ fmt.Println("type error")
	}
	fmt.Println("v == ",v)
}
Copy the code

If the assertion succeeds, the assertion is correct, and v is assigned to the value of the actual variable

Don’t try to turn mindlessly

func main(a) {
	var a Animal = "xiaomotong"

	v := a.(int)
	fmt.Println("v == ",v)
}
Copy the code

The above writing method, if not determine whether the assertion of success, direct strong, the program will panic, the implementation of the above program effect is as follows:

>go run main.go
panic: interface conversion: main.Animal is string, not int

goroutine 1 [running]:
main.main()
        D:/mycode/my_new_first/interface_test/main.go:13 +0x4c
exit status 2

Copy the code

The program crashes, so that’s an online problem XDM, something to watch out for here

And of course we could write it this way

type Animal interface{}

func main(a) {
	var a Animal = "xiaomotong"

	switch a.(type) {

	case string:
		fmt.Println("a type is string")
	case int:
		fmt.Println("a type is int")}}Copy the code

Determine the data type of the interface through the switch mode, and perform different tasks according to different data types

Attention points for reflection

Golang has the concept of reflection. In simple terms, we can use reflection packages to achieve the real type of interface, and real data

Here’s a quick example:

Use reflection to get the data type of the Animal interface, and the specific data value

type Animal interface{}

func main(a) {
	var a Animal = "xiaomotong"
	v := reflect.ValueOf(a)
	fmt.Printf("v type == ", v.Type(), " v == ", v.String())
}
Copy the code

The result of executing the above code is as follows:

> go run main.gov type == %! (EXTRA *reflect.rtype=string, string= v == , string=xiaomotong)Copy the code

There is nothing wrong with getting it normally, but if we write it this way, can we do it correctly?

type Animal interface{}

func main(a) {
	var a Animal = "xiaomotong"
	v := reflect.ValueOf(a)
	fmt.Printf("v type == ", v.Type(), " v == ", v.Int())
}
Copy the code

This example is very different from the above example. The output is v.tring () and v.int (), but the result is very different

go run main.go v type == %! (EXTRA *reflect.rtype=string, string= v == , string=xiaomotong) D:\mycode\my_new_first\interface_test>go run main.go panic: reflect: call of reflect.Value.Int on string Value goroutine 1 [running]: reflect.Value.Int(...) C:/Program Files/Go/src/reflect/value.go:999 main.main() D:/mycode/my_new_first/interface_test/main.go:26 +0x2bd exit status 2Copy the code

A panic error was reported by the go program when a string value was reflected as an Int value

Small knowledge, big challenge, simple write 2 matters needing attention, remind XDM

Welcome to like, follow and favorites

Friends, your support and encouragement, I insist on sharing, improve the quality of the power

All right, that’s it for this time

Technology is open, our mentality, should be more open. Embrace change, live in the sun, and strive to move forward.

I am Nezha, welcome to like, see you next time ~