'How do I concatenate a string and an enum in golang? [duplicate]

I have the following structure.

package main

type MyEnum int

const (
    Foo MyEnum = iota
    Bar
)

func (me MyEnum) String() string {
    return [...]string{"Foo", "Bar"}[me]
}

type MyStruct struct {
    Field1 MyEnum
    field2 MyEnum
    Field3 string
}

func main() {
    info := &MyStruct{
        Field1: Foo,
        field2: Bar,
        Field3: "hello"
    }

    fmt.Printf(info.Field3 + info.Field1) //error here
}

The error is this :

invalid operation: info.Field3 + info.Field1 (mismatched types string and MyEnum)

So how do I concatenate a string and the string representation of an enum?



Solution 1:[1]

Change

fmt.Printf(info.Field3 + info.Field1)

to

fmt.Printf("%v%v", info.Field3, info.Field1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Suyash Medhavi