Reputation: 5241
func lengthOfLongestSubstring(s string) {
match := make(map[string]int)
var current string
current = s[0]
if match[current] == 1 {
///
}
}
Why do I get the error cannot use s[0] (type byte) as type string in assignment
? As far as I can tell it's clear that s
is of type string
, why does accessing a character turn it into type byte
?
Upvotes: 4
Views: 13693
Reputation: 306
Let's read how one of the Go designers look at strings in go:
In Go, a string is in effect a read-only slice of bytes. If you're at all uncertain about what a slice of bytes is or how it works, please read the previous blog post; we'll assume here that you have.
It's important to state right up front that a string holds arbitrary bytes. It is not required to hold Unicode text, UTF-8 text, or any other predefined format. As far as the content of a string is concerned, it is exactly equivalent to a slice of bytes.
So, apparently in your case, s[0]
is a byte type, you need explicit case if you really need the assignment.
Upvotes: 5
Reputation: 708
func lengthOfLongestSubstring(s string) {
strArr:=[]rune(s)
fmt.Println(string(strArr[0]))
}
Upvotes: 8