5
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?

9

2 Answers 2

8
func lengthOfLongestSubstring(s string) {
    strArr:=[]rune(s) 
    fmt.Println(string(strArr[0]))

}
Sign up to request clarification or add additional context in comments.

Comments

6

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.