Types?
-
Go is statically typed language means every variable's type is fixed at
compile time, which helps catch errors early and makes code reliable.
Types of types
| Type | Description |
|---|---|
| 1. Basic Type |
int, float64, bool, string
|
| 2. Aggregate Types |
1. Arrays:An array is a fixed-size sequence of elements of
the same type.
2. struct
|
| Reference Types |
Pointers: variable that stores the memory address of another
variable (&) slices: maps functions channels |
4. Interface
-
Interface only declares the functions those should be implemented by
concrete types
This is similar to Abstract Class C++98, Concepts in C++20
In C++, we design our class hierarchy first. In Go, you often design your interfaces. These are Binded at Dynamic(ie runtime).
In Go, the philosophy is to "accept interfaces, return structs."
// Interface declares functions to be implemented by concrete types
type Speaker interface {
Speak() string
}
// Concrete Struct A
type Human struct{}
func (h Human) Speak() string { return "Hello!" }
// Concrete Struct B
type Dog struct{}
func (d Dog) Speak() string { return "Woof!" }
// This function doesn't care if it gets a Human or a Dog
// accept interfaces, return structs
func MakeItTalk(s Speaker) {
fmt.Println(s.Speak())
}
Interface Pollution Problem
-
Creating interfaces where they aren't actually needed. ie creating
interface for every class
Symptoms of Interface Pollution
1. One-to-One Mapping: Every single struct has a corresponding interface
2.Harder Navigation: In an IDE, clicking "Go to Definition" on a method takes you to the interface definition rather than the actual code doing the work.
| Polluted Interface | Good Interface |
|---|---|
|
|