mardi 25 juin 2019

panic: runtime error: index out of range in Golang

I have a function, which has to assign the index corresponding to rune of character to 1. I have a slice of alphabet, another slice of bytes with length of alphabet and for example, word "adel" will be assigned in byte slice as [100110000001000000....] like that, cause a-to first index, d-fourth index and so on. But if in the word 2 character of 'a' it has to be shifted to the next block of alphabet slice. How I have to set the function to this logic?

I have used for the beginning only slice of 2 blocks of alphabet and made length of 52 bits. Right now, I have this error:

''''
panic: runtime error: index out of range

goroutine 1 [running]:
main.getVector(0xc000080000, 0x1a, 0x1a, 0xc000080000, 0x1a, 0x1a)
        C:/Users/agi_a/code/CARDS/stem.go:90 +0x25b
main.main()
        C:/Users/agi_a/code/CARDS/main.go:18 +0x156
exit status 2
''''
{
<!-- language: golang -->: 

var alphabet = []string{
    "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
    "s", "t", "u", "v", "w", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
    "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
}
var a = make([]byte, len(alphabet))

func converToChar(s string) (r rune) {
    new, _ := utf8.DecodeRuneInString(s)
    switch {
    case 97 <= new && new <= 122:
        return new - 96
    case 65 <= new && new <= 90:
        return new - 64
    default:
        return r
    }
}

func getVector(s []string) []byte {
    for _, alph := range alphabet {
        for _, str := range s {
            if str == alph {
                if a[converToChar(alph)-1] == 0 {
                    a[converToChar(alph)-1] = 1
                } else if a[converToChar(alph)-1] == 1 {
                    a[converToChar(alph)-25] = 1
                }

            }
        }
    }
    return a
}

}

I expect that slice of byte for word 'adele' will be as [10011000001000000....00001......] as in the next block second 'e' will be assigned to index as 1.

Aucun commentaire:

Enregistrer un commentaire