Search code examples
goundefinedidentifier

Undefined global var


package main

import (
    "crypto/rand"
    "fmt"
)

var a = []byte{0, 0}

var b, c []byte

func gens(n int) ([]byte, error) {
    b := make([]byte, n)
    _, err := rand.Read(b)
    // Note that err == nil only if we read len(b) bytes.
    if err != nil {
        return nil, err
    }
    return b, nil
}

func main() {
    key, _ := gens(30)

    fmt.Printf("len %d key \n%x\n", len(key), key)
    for s := 1; s <= len(key); s++ {
        b := make([]byte, len(key))
        b = key[s:]
        c = key[:s]

        fmt.Printf("%d %x %x\n", s, b, c)
        // b = append(b, с...)
        // fmt.Printf("len %d key %x\n",len(d), d)
    }
}

Hello everybody! I want to create a function for cyclic rotation of an array of bytes, I take a simple function byte generator, and then try to rotate them byte-by-bye when I print - everything looks fine, but when I try to make an append(commented out 2 lines), I get Undefined с:

# command-line-arguments ./prepareKey.go:33:17: undefined: с

What's wrong!?? Forgive me if it was already there, poke me, I'm in a stupor from this! var c this global...


Solution

  • The problem is that the variable name and the identifier you use in append visually look the same, but they are different characters!

    If you print their bytes :

    fmt.Println([]byte("c")) // var b, c []byte
    fmt.Println([]byte("с")) // b = append(b, с...)
    

    Output is (try it on the Go Playground):

    [99]
    [209 129]
    

    Fix: Delete that 'c' lookalike, and write a "normal" 'c' character.

    Once you do it, the app compiles and runs fine. Try it on the Go Playground.