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.

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

报告相同问题?

悬赏问题

  • ¥15 python变量和列表之间的相互影响
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)