dpxf81245 2013-12-21 03:19
浏览 21
已采纳

在Go中从本地var中消除包名称的歧义

Is there a good way to disambiguate between a package name and a local variable? I'd rather not refactor the import name or variable name if I don't have to. Take for example...

import "path"

func foo() {
    path := "/some/path"
    // Disambiguate here
    path.Join(path, "/some/other/path")
}
  • 写回答

2条回答 默认 最新

  • doudi8231 2013-12-21 03:31
    关注

    The local variable always overrides (shadows) the package here. Pick another variable name, or alias the package as something else:

    http://play.golang.org/p/9ZaJa5Joca

    or

    http://play.golang.org/p/U6hvtQU8dx

    See nemo's alternatives in the other answer. I think the most maintainable way is to pick a variable name that won't overlap with the package name.

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

报告相同问题?

悬赏问题

  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型