I am a newbee to golang, and I write a program to test io package:

func main() {
    readers := []io.Reader{
         strings.NewReader("from string reader"),
         bytes.NewBufferString("from bytes reader"),
    }

    reader := io.MultiReader(readers...)
    data := make([]byte, 1024)

    var err error
    //var n int

    for err != io.EOF {
        n, err := reader.Read(data)
        fmt.Printf("%s\n", data[:n])
    }
    os.Exit(0)
}

The compile error is "err declared and not used". But I think I have used err in for statement. Why does the compiler outputs this error?

errerr:=err

这篇关于为什么golang编译器认为变量已声明但未被使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!