Brief Introduction to Interfaces in Go
Tags: go, programming, beginners
Published: 2018-07-25 13:54:57

---
title: Brief Introduction to Interfaces in Go
published: true
description: Brief introduction to interfaces in Go.
tags: go, programming, beginner
---

As a new Gopher and a CodeNewbie who has never typed C++ or any strongly-typed languages before, I was having a hard time looking for some beginners-friendly resources about Interfaces in Go. 

So here I am, decided to write one and doing my best to help any beginners.

***

# What is Interfaces?
 > *Interfaces in Go provide a way to specify the behavior of an object: if something can do this, then it can be used here. - Effective Go*

Well it basically means interfaces is a type that has a bunch of methods.

Alright without talking too much, lets make things clear today. 


## Write some code
**Aim**: To implement two methods `sum()` and `substr()` which will then print out the sum and subtraction of two numbers.

1) Let's start with the setup
```go
package main

import "fmt"

func main() {
}
```

2) Then the type, here we have `item1` and `item2` with two numbers type `float64` respectively: 
```go
package main

import "fmt"
```
```go
type item1 struct {
	num1, num2 float64
}

type item2 struct {
	num1, num2 float64
}

func main() {
}
```

3) Now we need some instructions (methods) for the code to work:
```go
package main

import "fmt"

type item1 struct {
	num1, num2 float64
}

type item2 struct {
	num1, num2 float64
}
```
```go
// Method
func (a item1) sum() float64 {
	return a.num1 + a.num2
}

// Method
func (a item1) substr() float64 {
	return a.num1 - a.num2
}

// Method
func (b item2) sum() float64 {
	return b.num1 + b.num2
}

// Method
func (b item2) substr() float64 {
	return b.num1 - b.num2
}
```
```go
func main() {
}
```

Before continuing, let's get a better understanding on the method.
```go
// Method
func (b item2) substr() float64 {
	return b.num1 - b.num2
}
```

`b` is a reference to `item2`, which is the way we access `num1` and `num2` inside `item2`. Then we have the name `substr()`, the return type `float64` and the return method.


4) Implementing `result1` and `result2` and print out the result:
```go
package main

import "fmt"

type item1 struct {
	num1, num2 float64
}

type item2 struct {
	num1, num2 float64
}

// Method
func (a item1) sum() float64 {
	return a.num1 + a.num2
}

// Method
func (a item1) substr() float64 {
	return a.num1 - a.num2
}

// Method
func (b item2) sum() float64 {
	return b.num1 + b.num2
}

// Method
func (b item2) substr() float64 {
	return b.num1 - b.num2
}
```
```go
// Result 1 & Result 2
func result1(i item1) {
	fmt.Println("Sum: ", i.sum())
	fmt.Println("Substr: ", i.substr())
}

func result2(i item2) {
	fmt.Println("Sum: ", i.sum())
	fmt.Println("Substr: ", i.substr())
}

func main() {
	a := item1{num1: 10, num2: 5}
	b := item2{num1: 20, num2: 10}

	result1(a) 
	result2(b)
}

// Sum:  15
// Substr:  5
// Sum:  30
// Substr:  10
```

Here we passed two variables: `a` and `b` to two functions: `result1` and `result2`, and got the results.

 
But here is the problem: 
> *Why do we need to write two functions (`result1` and `result2`) that basically do the same thing?*


---


### So here is the thing you have been waiting for: Interfaces
5) Implementing interface. I mentioned before, interface is a type that has a collection of methods (function). It has both `sum()` and `substr()`, which are the names of the methods below:
```go
package main

import "fmt"
```
```go
type math interface {
	sum() float64
	substr() float64
}
```
```go
type item1 struct {
	num1, num2 float64
}

type item2 struct {
	num1, num2 float64
}

// Method
func (a item1) sum() float64 {
	return a.num1 + a.num2
}

// Method
func (a item1) substr() float64 {
	return a.num1 - a.num2
}

// Method
func (b item2) sum() float64 {
	return b.num1 + b.num2
}

// Method
func (b item2) substr() float64 {
	return b.num1 - b.num2
}

func main() {

}
```

6) Print out the result:
```go
package main

import "fmt"

type math interface {
	sum() float64
	substr() float64
}

type item1 struct {
	num1, num2 float64
}

type item2 struct {
	num1, num2 float64
}

// Method
func (a item1) sum() float64 {
	return a.num1 + a.num2
}

// Method
func (a item1) substr() float64 {
	return a.num1 - a.num2
}

// Method
func (b item2) sum() float64 {
	return b.num1 + b.num2
}

// Method
func (b item2) substr() float64 {
	return b.num1 - b.num2
}
```
```go
func result(m math) {
	fmt.Println("Sum: ", m.sum())
	fmt.Println("Substr: ", m.substr())
}

func main() {
	a := item1{num1: 10, num2: 5}
	b := item2{num1: 20, num2: 10}

	result(a)
	result(b)
}

// Sum:  15
// Substr:  5
// Sum:  30
// Substr:  10
```
So now we just have to include interface `math` with `result`, we will then be able to access `sum()` and `substr()` by calling it once in the `main()` section. 
Here we are calling `result` once for both variables and got the same results! 

Continue -> [Empty interface](https://dev.to/shearywtan/continue---empty-interfaces-in-go-219e)

> *Thanks for reading my first ever published article!*