douzhao6584 2011-09-07 05:50
浏览 69
已采纳

去编译错误:未定义的变量

new to programming / even newer to go. having trouble with a small go program - will not compile with undefined variable errors. the code:

package main

import (
  "fmt"
  "io"
  "os"
)

const file = "readfile.txt" 
var s string

func lookup(string) (string, string, string) {
    artist := s
    album := s
    year := s

    return artist, album, year
}

func enterdisk() (string, string, string) {
  var artist string
    var album string
    var year string

    println("enter artist:")
  fmt.Scanf("%s", &artist)

    println("enter album:")
  fmt.Scanf("%s", &album)

    println("enter year:")
  fmt.Scanf("%s", &year)

    return artist, album, year
}

func main() {

  println("enter UPC or [manual] to enter information manually:")
  fmt.Scanf("%s", &s)

    s := s
  switch s { 
        case "manual
": artist, album, year := enterdisk()
        default: artist, album, year := lookup(s)
  }

    f,_ := os.OpenFile(file, os.O_APPEND|os.O_RDWR, 0666) 
  io.WriteString(f, (artist + ", \"" + album + "\" - " + year + "
")) 

    f.Close()
    println("wrote data to file")
}

and the errors:

catalog.go:49: undefined: artist
catalog.go:49: undefined: album
catalog.go:49: undefined: year

however, those variables will not be defined until the code runs. additionally, the "lookup" function is not yet written, which it just returns what it is passed. i know the lookup and enterdisk functions work on their own, but i am trying to test the switch statement.

i have tried declaring the variables in main, however i get this error:

catalog.go:49: artist declared and not used
catalog.go:49: album declared and not used
catalog.go:49: year declared and not used

p.s. i have read http://tip.goneat.org/doc/go_faq.html#unused_variables_and_imports , and i agree that if this is only semantics, i still want to fix it. i just don't know how!

  • 写回答

2条回答 默认 最新

  • dotif64826 2011-09-07 12:46
    关注

    Read about blocks and declarations and scope in Go.

    Each clause in a switch or select statement acts as an implicit block.

    Blocks nest and influence scoping.

    The scope of a declared identifier is the extent of source text in which the identifier denotes the specified constant, type, variable, function, or package.

    The scope of a constant or variable identifier declared inside a function begins at the end of the ConstSpec or VarSpec (ShortVarDecl for short variable declarations) and ends at the end of the innermost containing block.

    switch s { 
        case "manual
    ": artist, album, year := enterdisk()
        default: artist, album, year := lookup(s)
    }
    . . .
    io.WriteString(f, (artist + ", \"" + album + "\" - " + year + "
    ")) 
    

    The scope of the short variable declarations of the artist, album, and year variables in the switch case and default case clauses begins and ends within each clause (the innermost containing block). The artist, album, and year variables no longer exist and are not visible to the WriteString() statement.

    Instead, write:

    var artist, album, year string
    switch s {
    case "manual
    ":
        artist, album, year = enterdisk()
    default:
        artist, album, year = lookup(s)
    }
    . . .
    io.WriteString(f, (artist + ", \"" + album + "\" - " + year + "
    "))
    

    Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared in the same block with the same type, and at least one of the non-blank variables is new. As a consequence, redeclaration can only appear in a multi-variable short declaration.

    Therefore, the artist, album, and year variables are no longer declared (and assigned) using short variable declarations inside the switch case clauses because that would hide the variable declarations in the outer block, they are merely assigned.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错