If I have a type defined as a string constant, why can't I pass that type
to functions as an actual string?
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))
}
func myFunc(a StringType, b StringType) bool {
return strings.Contains(a, b)
//return false
}
No comments:
Post a Comment