dsfovbm931034814 2014-07-14 07:16
浏览 128
已采纳

Golang:“ package ast_test”下划线测试

Source file from Golang's stdlib

File's base directory: ast

Package specified in the file: ast_test ???

Package specified in all other files inside the same directory: ast

From golang.org: src contains Go source files organized into packages (one package per directory) ... By convention, packages are given lower case, single-word names; there should be no need for underscores or mixedCaps ... Another convention is that the package name is the base name of its source directory

How is it possible to have multiple packages (here 2) in one folder?

  • 写回答

1条回答 默认 最新

  • dsjxgu4759 2014-07-14 07:31
    关注

    You find another example in src/pkg/go/ast/commentmap_test.go, with the comment:

    // To avoid a cyclic dependency with go/parser, this file is in a separate package.
    

    I suppose it allows for an othogonal command like:

    go test
    

    That will test parser features while avoiding for that test to be part of the same parser features (since it has been put in a separate package)

    From go command man page:

    Test files that declare a package with the suffix "_test" will be compiled as a separate package, and then linked and run with the main test binary.


    This thread asked the question:

    Now that the go tool requires each directory to be one package and doesn't allow to have files with different package names inside the same folder, how is the package keyword useful? It seems like a unnecessary repetition.

    Is it required by the compiler or is there any plan to remove it?

    The answers hinted at the fact that you can have more than one package in a folder:

    The package declaration declares the name of the package.
    The language Go doesn't know what a file or a directory is and the import path itself doesn't effect the actual name of the package that is being imported. So the only way the compiler knows what to call the package is the package declaration.

    The language doesn't require separate packages to be in separate directories; it is a requirement of the go tool.
    Another hypothetical implementation may not have this requirement.

    Even this go tool requirement can be bypassed thanks to the "// +build" build tags.

    For example, read misc/cgo/gmp or misc/cgo/stdio (some files include // +build ignore)

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

报告相同问题?

悬赏问题

  • ¥15 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式