GoLang Tutorial - Interfaces
Interfaces are named collections of methods:
type Shape interface { area() float64 }
So, in the above code, the Shape interface has a method called area().
We can add another method called perimeter like this:
type Shape interface { area() float64 perimeter() float64 }
In the following sample, we will see a basic interface for geometric shapes.
The code: interfaces.go
Notice that we're passing an interface (s Shape) but not a specific implementation (neither c Circle nor r Rectangle):
The first time I saw this was the official source code for the linked list (https://golang.org/src/container/list/list.go):
// Element is an element of a linked list. type Element struct { // Next and previous pointers in the doubly-linked list of elements. // To simplify the implementation, internally a list l is implemented // as a ring, such that &l.root is both the next element of the last // list element (l.Back()) and the previous element of the first list // element (l.Front()). next, prev *Element // The list to which this element belongs. list *List // The value stored with this element. Value interface{} }
The interface{} type is the interface that has no methods. So, if we write a function that takes an interface{} value as a parameter, we can supply that function with any value. Given the following:
func foo(Value interface{}) { // ... }
The function will accept any parameter whatsoever. Again, the type of Value is interface{}. Confusing? Check How to use interfaces in Go.
Here is an example of an empty interface:
The code: empty-interface.go
Another practical example is:
func Marshal(v interface{}) ([]byte, error)
The function returns the JSON encoding form of v.
With a given Go data structure, Message:
type Message struct { Name string Body string Time int64 }
and an instance of Message
m := Message{"Interface", "Empty Interface", 1556747623}
We can marshal a JSON-encoded version of m using json.Marshal:
b, err := json.Marshal(m)
If all is well, err will be nil and b will be a []byte containing this JSON data:
b == []byte(`{"Name":"Interface","Body":"Empty Interface","Time":1556747623}`)
Here is the full code:
The code: json-marshall.go
Go Tutorial
- GoLang Tutorial - HelloWorld
- Calling code in an external package & go.mod / go.sum files
- Workspaces
- Workspaces II
- Visual Studio Code
- Data Types and Variables
- byte and rune
- Packages
- Functions
- Arrays and Slices
- A function taking and returning a slice
- Conditionals
- Loops
- Maps
- Range
- Pointers
- Closures and Anonymous Functions
- Structs and receiver methods
- Value or Pointer Receivers
- Interfaces
- Web Application Part 0 (Introduction)
- Web Application Part 1 (Basic)
- Web Application Part 2 (Using net/http)
- Web Application Part 3 (Adding "edit" capability)
- Web Application Part 4 (Handling non-existent pages and saving pages)
- Web Application Part 5 (Error handling and template caching)
- Web Application Part 6 (Validating the title with a regular expression)
- Web Application Part 7 (Function Literals and Closures)
- Building Docker image and deploying Go application to a Kubernetes cluster (minikube)
- Serverless Framework (Serverless Application Model-SAM)
- Serverless Web API with AWS Lambda
- Arrays vs Slices with an array left rotation sample
- Variadic Functions
- Goroutines
- Channels ("<-")
- Channels ("<-") with Select
- Channels ("<-") with worker pools
- Defer
- GoLang Panic and Recover
- String Formatting
- JSON
- SQLite
- Modules 0: Using External Go Modules from GitHub
- Modules 1 (Creating a new module)
- Modules 2 (Adding Dependencies)
- AWS SDK for Go (S3 listing)
- Linked List
- Binary Search Tree (BST) Part 1 (Tree/Node structs with insert and print functions)
- Go Application Authentication I (BasicAuth, Bearer-Token-Based Authentication)
- Go Application Authentication II (JWT Authentication)
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization