WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Go · Advanced · question 55 of 100

Can you use generics in Go? If so, how?

📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

Yes, you can use generics in Go, starting from Go 1.18 which introduced support for generics. Generics allow you to write functions and data structures that can work with different types while maintaining the type safety of your code. This can lead to more reusable and easier-to-read code.

Here’s a brief introduction to using generics in Go, along with examples.

To define a generic function or type, you first need to define a type constraint. Type constraints are special interfaces that specify the restrictions on types that can be used as generic arguments. For example, the following type constraint ensures that the type supports the addition, subtraction, multiplication, and division operations:

type Numeric interface {
    type int, float64
}

Once you have your type constraint, you can define a generic function using that constraint. Here's an example of a generic `Sum` function that accepts a slice of numeric values and works with both `int` and `float64` types:

func Sum[T Numeric](values []T) T {
    var result T
    for _, v := range values {
        result += v
    }
    return result
}

To use this generic function in your code, you can call it like a regular function:

func main() {
    intSlice := []int{1, 2, 3, 4, 5}
    floatSlice := []float64{1.1, 2.2, 3.3, 4.4, 5.5}

    intSum := Sum(intSlice)
    fmt.Println("Sum of integers:", intSum) // Output: Sum of integers: 15

    floatSum := Sum(floatSlice)
    fmt.Printf("Sum of floats: %.1fn", floatSum) // Output: Sum of floats: 16.5
}

Here’s another example that demonstrates how to use generics with custom types and interfaces. Consider an ‘Adder‘ interface and two custom types (‘MyInt‘ and ‘MyFloat‘) that implement this interface:

type Adder[T any] interface {
    Add(T) T
}

type MyInt int

func (a MyInt) Add(b MyInt) MyInt {
    return a + b
}

type MyFloat float64

func (a MyFloat) Add(b MyFloat) MyFloat {
    return a + b
}

You can define a generic ‘GenericSum‘ function that accepts slices of values conforming to the ‘Adder‘ interface:

func GenericSum[T Adder[T]](values []T) T {
    var result T = values[0]
    for _, v := range values[1:] {
        result = result.Add(v)
    }
    return result
}

Finally, you can use this ‘GenericSum‘ function with your custom types:

func main() {
    myIntSlice := []MyInt{1, 2, 3, 4, 5}
    myFloatSlice := []MyFloat{1.1, 2.2, 3.3, 4.4, 5.5}

    myIntSum := GenericSum(myIntSlice)
    fmt.Println("Sum of MyInt values:", myIntSum) // Output: Sum of MyInt values: 15

    myFloatSum := GenericSum(myFloatSlice)
    fmt.Printf("Sum of MyFloat values: %.1fn", myFloatSum) // Output: Sum of MyFloat values: 16.5
}

Keep in mind that this is just a brief introduction to generics in Go, and there’s more to learn. For comprehensive information and examples, refer to the [official Go blog post about generics]
(https://go.dev/blog/generics-proposal).

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Go interview — then scores it.
📞 Practice Go — free 15 min
📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

All 100 Go questions · All topics