Go的”import”语法是否特殊和独特?

huangapple go评论78阅读模式
英文:

Is the Go "import" syntax special and unique?

问题

http://golang.org/ref/spec#Import_declarations

import "package1"

import ("package1";"package2")

import (
        "package1"
        "package2"
       )

“import”语法是专门用于导入包的吗?它看起来像是一个使用“;”而不是“,”进行参数化的函数调用。它甚至看起来可以是一个类似于Ruby的方法调用(即:没有“()”)。

PS
我想问这个问题是为了获得一些观点。我非常喜欢使用Go,但它的一些语法似乎有点不一致,有时候冗长。这让我想知道是否很难像C++对C进行预处理一样,为Go创建一个预处理器来清理一些语法。称之为Go++(更快的Go)。我在想Objective C 2.0是改进程序员工作流程的一个很好的例子(例如:字典/数组/数字字面量)。Go++在概念上也类似于elixir-lang.org(在Erlang之上的语法增强)。

英文:

http://golang.org/ref/spec#Import_declarations

import "package1"

import ("package1";"package2")

import (
        "package1"
        "package2"
       )

Is the "import" syntax something especially made for importing packages? It looks like a function call that uses ";" instead of "," for parameterization. It seems that it could even have been a ruby-esque method call (ie: sans "()")

PS
Thought I'd ask this to get some perspective. I'm really enjoying working with Go, but some of its syntax seems a bit inconsistent and sometimes verbose. Makes me wonder how hard it would be to create a pre-processor for it like C++ is/was to C to clean up some of the syntax. Call it Go++ (go faster). I'm thinking of Objective C 2.0 as a great example to improving the programmer's workflow (eg: dictionary/array/number literals). Go++ would also be similar in concept to exlixir-lang.org (a syntactical enhancement on top of Erlang).

答案1

得分: 2

是的,import 是特殊的。它必须位于文件的顶部,并且它的参数总是字面值。这是必须的,因为Go语言在尝试管理依赖项以加快构建时间方面做了很多工作。

它也不能只是一个普通的环境,因为它会将变量注入到当前作用域中,这是其他函数无法做到的。

至于语法,是的,有些部分确实不太令人愉快。但对我和我遇到的大多数其他编写Go语言的人来说,这只是一个相对较小的问题。好处是,语法非常简单,易于解析,因此工具相对简单,这就是为什么我担心仅仅为了几个按键而改变语法的原因。

英文:

Yes import is special. It's required to be at the top of the file and it's arguments are always literals. This has to happen because go does a lot with trying to manage dependencies to speed up build times.

It also can't just be a regular environment because it injects variables into the current scope, something no other function can do.

As for the syntax, yeah some parts are not exactly pleasant. But it's a pretty minor concern to me and most other people who write Go that I've encountered. On the upside, the syntax is dead simple to parse so tooling is relatively straightforward which is why I worry about just changing the syntax for a few keystrokes.

huangapple
  • 本文由 发表于 2013年11月5日 05:43:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/19777736.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定