dqoqnmb163241
dqoqnmb163241
2013-09-06 14:22

如果我有一个定义为字符串常量的类型,为什么不能将该类型传递给实际的字符串呢?

  • IT行业问题
  • 计算机技术
  • it技术
  • 编程语言问答
  • 互联网问答
已采纳

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
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • dongre9937 dongre9937 8年前

    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.

    点赞 评论 复制链接分享
  • dongyu4908 dongyu4908 8年前

    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))
    
    点赞 评论 复制链接分享
  • duanfang5849 duanfang5849 8年前

    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))
    
    点赞 评论 复制链接分享

为你推荐