19

I'm in the process of learning Go and the documentation and interactive lessons say that an empty interface can hold any type, as it requires no additionally implemented methods.

So for an example:

func describe(i interface{}) {
    fmt.Printf("Type: %T | Value: %v\n", i, i)
}

...would print out...

"Type: int | Value: 5" // for i := 5
"Type: string | Value: test" // for i := "test"
... etc

So I guess my question is if this is Go's way of implementing generic functions or if there is another, more suitable, way of doing them.

4
  • 3
    Go doesn't have true generics so this is a way around it. This system breaks down though when you have to do type assertions to extract data from composite types or enforce other rules. Commented Mar 10, 2017 at 20:19
  • @squiguy so to achieve a quasi-generic, you essentially use empty interfaces with type switches? Commented Mar 10, 2017 at 20:24
  • It's hard to say that's 100% correct, but that's one way. Unless you absolutely want to do that I would try and avoid it. Commented Mar 10, 2017 at 22:02
  • Please see @mayank's answer below. There is now a draft proposal for Generics in Go Commented Dec 28, 2020 at 16:13

3 Answers 3

32

As of Go 1.18 you can write a generic function Print as below:

package main

import (
    "fmt"
)

// T can be any type
func Print[T any](s []T) {
    for _, v := range s {
        fmt.Print(v)
    }
}

func main() {
    // Passing list of string works
    Print([]string{"Hello, ", "world\n"})

    // You can pass a list of int to the same function as well
    Print([]int{1, 2})
}

Output:

Hello, world
12
Sign up to request clarification or add additional context in comments.

1 Comment

An important development +1. I mention something similar. See stackoverflow.com/a/63353886/12817546
19

The Go paradigm is generally to avoid this by implementing the behavior in non-empty interfaces. For example, say you wanted to print something with type-specific formatting:

func Print(i interface{}) {
    switch o := i.(type) {
        case int64:
            fmt.Printf("%5d\n", o)
        case float64:
            fmt.Printf("%7.3f\n", o)
        case string:
            fmt.Printf("%s\n", o)
        default: // covers structs and such
            fmt.Printf("%+v\n", o)
    }
}

Alternatively, you could define an interface for things that know how to string themselves (this exists in the base library as an fmt.Stringer), and use that:

type Stringer interface {
    String() string
}

func Print(o Stringer) {
    fmt.Println(o.String())
}

type Foo struct {
    a, b int
}

func (f Foo) String() string {
    // Let's use a custom output format that differs from %+v
    return fmt.Sprintf("%d(%d)", f.a, f.b) 
}

type Bar struct {
    t bool
}

func (b Bar) String() string {
    if b.t {
        return "TRUE! =D"
    }
    return "false =("
}

https://play.golang.org/p/Ez6Hez6cAv

This lets you have a generic-like functionality, but still retain type safety and have the behavior itself defined by the types, rather than your generic function.

Go encourages you to think of types in this way, based on their behavior, what they can do rather than what they contain.

Comments

0

Golang doesn't have a generic type, so the way you get around this is passing interface type and using type switches within the function.

1 Comment

no need for type switches ... see the 2nd half of answer by @Kaedys

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.