如果我有一个定义为字符串常量的类型,为什么不能将该类型传递给实际的字符串呢?
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
}
这是我的意思:去游乐场 p>
“ StringType”类型是字符串,仅此而已。 编译器抱怨第21行,但是由于某种原因,第16行可以正常工作(如果您注释21和取消注释22)。 p>
这两行之间有什么区别(因为 将StringType传递给相同的函数),为什么一个有效而另一个无效? p>
以下是内联代码: p>
包main
import(
“ fmt”
“ strings”
)
type StringType字符串
const(
FirstString =“ first”
SecondString =“ second”
)\ n
func main(){
fmt.Println(strings.Contains(FirstString,SecondString))//第16行
}
func myFunc(a StringType,b StringType)bool {
返回字符串。 包含(a,b)//第21行
//返回false
}
code> pre>
div>
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))