Here's what I mean: Go playground
The "StringType" type is a string and nothing more. The compiler complains about line 21, but for some reason, line 16 works without issue (if you comment 21 and uncomment 22).
What is the difference between those two lines (as both as passing StringType to the same function), and why does one work and the other not?
Here's the code, in-line:
package main
import (
"fmt"
"strings"
)
type StringType string
const (
FirstString = "first"
SecondString = "second"
)
func main() {
fmt.Println(strings.Contains(FirstString, SecondString)) // line 16
}
func myFunc(a StringType, b StringType) bool {
return strings.Contains(a, b) // line 21
//return false
}
Go has a strict typing system. StringType
and string
is not the same type, and you need to do a type conversion.
The Go specs states that:
A non-constant value x can be converted to type T in any of these cases:
x is assignable to T.
x's type and T have identical underlying types.
...
x is an untyped constant representable by a value of type T
Since StringType
has the underlying type of string
, this conversion is possible:
func myFunc(a StringType, b StringType) bool {
return strings.Contains(string(a), string(b))
//return false
}
Also, because FirstString
and SecondString
are untyped constants representable by StringType, this will allow you to pass it as a StringType
, as stated by the specs.
In the first case, you're passing strings, no problem.
In the second one, you pass StringType
values instead of strings. You need to tell the compiler they're strings by converting them :
Replace
return strings.Contains(a, b)
with
return strings.Contains(string(a), string(b))
Because StringType
is not of type string
it cannot be assigned to anything of type string
. Go is a strongly typed language. But as the underlying types of StringType
and string
are the same, you can use a conversion:
return strings.Contains(string(a), string(b))