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

chefcurry7
  • 4,813
  • 11
  • 28
  • 33
  • 2
    Does casting s[0] to string work? – DJSweetness Jun 15 '17 at 15:46
  • 3
    because `s[0]` is a `byte`, not a `string`. Are you certain that you want to operate only on bytes or should this work with utf8 runes? – JimB Jun 15 '17 at 15:47
  • @JimB i just want to retrieve character so I can use it as a key on a map – chefcurry7 Jun 15 '17 at 15:49
  • wait, silly question, i could have just changed the map to make it map[byte]int. please write answer so i can accept – chefcurry7 Jun 15 '17 at 15:50
  • @chefcurry7: `s[0]` isn't going to return the expected character from `lengthOfLongestSubstring("世界")`. If you want to change `s[0]` to a `string`, convert it to a `string`. – JimB Jun 15 '17 at 15:50
  • 3
    Possible duplicate of [Access random rune element of string without using for … range](https://stackoverflow.com/questions/44527223/access-random-rune-element-of-string-without-using-for-range/44527543#44527543). – icza Jun 15 '17 at 15:53
  • So, aside from the questions of UTF8 versus bytes, generally if you're trying to look at each byte of a string _as a string itself_, what you want is subslicing, not an index of the string. For example, `s[n:n+1]` will give you a _string_ variable that holds just the nth byte of `s`, where `s[n]` gives you a _byte_ variable that holds that same information. If you want it to be UTF8 compatible, you can either use the utf8 package or simply convert the string to a `[]rune` first (or use a range loop using the `for i, v := range s` syntax). – Kaedys Jun 15 '17 at 16:50
  • 1
    Possible duplicate of [Access random rune element of string without using for ... range](https://stackoverflow.com/questions/44527223/access-random-rune-element-of-string-without-using-for-range) – Martin Tournoij Jun 15 '17 at 18:02
  • 1
    If you have solved your own question, provide your own answer. – Jonathan Hall Jun 15 '17 at 18:04

2 Answers2

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

}
Xian Shu
  • 672
  • 6
  • 8
5

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.

Community
  • 1
  • 1
Heissenberger
  • 294
  • 1
  • 7