dpymrcl269187540 2016-04-16 15:32
浏览 18
已采纳

为什么要使用双引号导入声明

As a go beginner this always gets me whenever I start a new source file. So go's package clause defines the package name, without double quotes, since the package name must be an identifier, it cannot contains invalid chars like space or something. However, when it comes to import declarations, the package name must be double quoted, as the package name is exactly the one used in package clause, it must be identifier too(of course / is allowed as separator). It looks to me that would only add more key strokes without other benefits. I wonder why it is designed this way that imports must be double quoted strings.

Also, if we look at other languages, #include <foo.h>, using System.Bar, import java.lang.moo none of them requires imports to be strings.

  • 写回答

1条回答 默认 最新

  • douquan9826 2016-04-17 00:40
    关注

    A path a/b/foo is more like a string than an identifier: identifiers don't have separators, and paths may contain characters that aren't allowed in identifiers. You say package names can't contain spaces, which is true, but paths can, just as package names can't contain periods (.), but paths can. For example:

    import "golang.org/x/exp/shiny/vendor/github.com/BurntSushi/xgb/render"`
    

    This is mostly the same as C, which is listed in the question as not using strings to specify #include paths, but shares similarities with the go import statement. The two forms are both string-like: #include <a/b/foo.h> and #include "a/b/foo.h" although one uses <> rather than quotes to delimit the string.

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

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)