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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题