duandun2218 2019-01-27 13:03
浏览 84
已采纳

命令行参数:处理“未定义”错误消息

Following is how the source files are organized

✘-1 ~/Go/src/github.com/krmahadevan/packages 
18:24 $ tree .
.
├── sample_main.go
└── sample_one.go

0 directories, 2 files

Here's how the source code looks like:

sample_one.go

package main

var data map[string]string

func init() {
    data = make(map[string]string, 0)
}

sample_one.go

package main

import "fmt"

func main() {
    data["foo"] = "bar"
    fmt.Println(data)
}

Now when I attempt at running sample_main.go I get errors stating that data is undefined.

18:24 $ go run sample_main.go 
# command-line-arguments
./sample_main.go:6:2: undefined: data
./sample_main.go:7:14: undefined: data
✘-2 ~/Go/src/github.com/krmahadevan/packages

But when I build the code into a binary and then execute it, it runs fine.

✔ ~/Go/src/github.com/krmahadevan/packages 
18:27 $ go build
✔ ~/Go/src/github.com/krmahadevan/packages 
18:28 $ ./packages 
map[foo:bar]
✔ ~/Go/src/github.com/krmahadevan/packages

I would like to understand why is this behavior ?

Environment:

18:31 $ go version
go version go1.11.4 darwin/amd64

The closest I found was this post : Golang : command-line-arguments undefined: variable

But this post talks about scoped variables that are defined in main.

But my problem statement involves variables defined in another go file and accessed in the main method.

  • 写回答

1条回答 默认 最新

  • dttphb59319 2019-01-27 13:10
    关注

    To understand why, read the go command documentation:

    Command go

    Compile and run Go program

    Usage:

    go run [build flags] [-exec xprog] package [arguments...]
    

    Run compiles and runs the named main Go package. Typically the package is specified as a list of .go source files, but it may also be an import path, file system path, or pattern matching a single known package, as in 'go run .' or 'go run my/cmd'.

    Compile packages and dependencies

    Usage:

    go build [-o output] [-i] [build flags] [packages]
    

    Build compiles the packages named by the import paths, along with their dependencies, but it does not install the results.

    If the arguments to build are a list of .go files, build treats them as a list of source files specifying a single package.

    For more about specifying packages, see 'go help packages'. For more about where packages and binaries are installed, run 'go help gopath'.


    go run: Typically the package is specified as a list of .go source files.

    For your go run example, list the files:

    go run sample_main.go sample_one.go
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵