skip to content
Alvin Lucillo

Go dependency pinning and anonymous field

/ 1 min read

💻 Tech

In Go, you can pin a version of a dependency in your go.mod file using the following syntax:

replace github.com/go-sql-driver/mysql => github.com/go-sql-driver/mysql v1.6.0

This forces the module to use the specified version of the dependency. One use is if there’s a vulnerability in a dependency, and you want to pin it to a version that doesn’t have the vulnerability until such time that you can upgrade to a newer version.

Also in Go, there’s what we call an anonymous embedded field. It’s a field that doesn’t have a name, and the struct that contains it can access the embedded field’s methods directly. For example:

type Character struct {
    Name string
    Age int
}

func (*Character) SayHello() {
    fmt.Println("Hello!")
}

type Film struct {
    Character
    Title string
}

func main() {
    f := Film{
        Character: Character{
            Name: "John Doe",
            Age: 21,
        },
        Title: "Software Engineer - A day in a life",
    }

    f.SayHello() // prints "Hello!"
}