# Go Interfaces
It is time to look at interfaces in Go. After that, you will briefly examine testing in Go.
Go offers the so-called "interface type". This is a collection of method signatures. An interface value can hold any value that implements those methods. Try it:
You see the declaration of the three types and methods as before. You have also declared an additional interface, Euclid, which includes a method signature Norm() float64. Since all defined types implement the Norm method, You can now use the Euclid interface to hold the instances of those types.
There is a special empty interface: interface{}. Because it has no method signatures, it is implemented by all types and can be used to hold values of any type:
The syntax for direct access to the underlying value of the interface value is i.(T). This is useful for type switches. In the next module, you will learn the control constructs.
# Simple unit test
Go offers the testing package testing and a tool called go test. These are very helpful.
To explore the basics, first, write a function sum. This is the function you will test:
You should be able to see what this does and know that it probably works. Even so, you should test it.
Save the previous program as sum.go in a folder sumutil. Then make another file with the following:
Save this file as sum_test.go. Now run go test.
You will see that it passes the test.
A test function has the syntax TestXXX.
A benchmark function has the syntax BenchXXX.
Use go test -help to see what you need to run benchmarks.
This video provides a quick demonstration of working with interfaces in Golang.
To summarize, this section has explored:
- How the "interface type" offered by Go is a collection of method signatures, and an interface value is used to implement those methods.
- How the
Euclidinterface can hold instances of all defined types implementing theNormmethod. - How the special empty interface
interface{}can be implemented by all types and hold values of any type because it has no method signatures. - How to use Go's testing package
testingand thego testtool.