# 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.
# Rob demonstrates Go interfaces
Further readings:
# Next up
Basics and interfaces are covered, what's next? Take a closer look at the control structures in Go by diving into if
, switch
, and for
statements in the next section.