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.

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

报告相同问题?