Adding behaviour to any type using Type Aliasing

To define methods on a type you don’t “own”, you need to define an alias for the type you want to extend:

package main

import (
    "fmt"
    "strings"
)

type MyStr string

func (s MyStr) Uppercase() string {
    return strings.ToUpper(string(s))
}

func main() {
    fmt.Println(MyStr("test").Uppercase())
}

http://www.golangbootcamp.com/book/methods

Attaching Method to any type

  • We can attach methods to any type in Go. For e.g.

    // Basic Types
    int
    string
    float64
    
    // Bare Types
    array
    struct
    
    // -----------------------
    // Do not use "Pointer Receivers" with below types since they already carry a pointer with themselves.
    // i.e. slice, map, chan, func
    // -----------------------
    // Pointer Bearing Types
    slice
    map
    chan // Channels
    
    // We can also attach methods to:
    func
    

Code Organization

Methods can be defined on any file in the package, but my recommendation is to organize the code as shown below: Define constants Define vars Define interface Define struct Implementation of methods on struct Example: package models // list of packages to import import ( "fmt" ) // list of constants const ( ConstExample = "const before vars" ) // list of variables var ( ExportedVar = 42 nonExportedVar = "so say we all" ) type Fly interface { Fly() string } // Main type(s) for the file, // try to keep the lowest amount of structs per file when possible. [Read More]

Implement Sort Interface

sort package defines an interface whose name is Interface: type Interface interface { // Len is the number of elements in the collection. Len() int // Less reports whether the element with // index i should sort before the element with index j. Less(i, j int) bool // Swap swaps the elements with indexes i and j. Swap(i, j int) } For slice, or any other collection types, provided that it implements the Len(), Less and Swap functions, you can use sort. [Read More]

Inheritance and Composition in GO

Structs can “inherit” from other structs by a method known as embeding. Here is a simple example: package main import ( "fmt" ) type a struct { Name string } //embeds a value of type a type b struct { a } //embeds a pointer to an a type c struct { *a } func main() { a := a{Name: "Janeway"} fmt.Println(a.Name) b := &b{a: a} fmt.Println(b.Name) c := &c{a: &a} fmt. [Read More]

Method receiver vs Pointer receiver

Struct values when used as method receivers are exactly that values and so are a shallow copy of the value allocated a new portion of memory. The effects are not observed outside of the method as there are no references to the new value and so it is garbaged collected. Pointer receivers allow mutation of what the pointer points to. Your function is recieving a pointer to the same address in memory even in the function stackframe. [Read More]

Methods in GO

Intro We have already seen golang functions. Now the question is: what is the difference between a function and a method in Go? Answer is - A method is a function that has a defined receiver, in OOP terms, a method is a function on an instance of an object. Go doesnt have classes but structs, and we can add receiver methods on structs to add behaviour. To add some behaviour to a struct you can have it be a method reciever. [Read More]

Interface in Go

Intro We declare an Interface much like as we define a user defined type. Interfaces decouple different types from each other so we can create more maintainable programs. More: An Interface is a Protocol, a Contract. Bigger the Interface the weaker the abstraction. –> Rob Pike It’s an abstract type. It doesn’t have any implementation. It only describes the expected behavior. The opposite of Abstract Type is Concrete Type. All the types in Go except Interface are of Concrete Type. [Read More]