在Go Playground中可以导入的包有哪些?

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

Which packages may be imported in the go playground?

问题

我在http://play.golang.org/上找不到可以导入的包的列表。我试图使用(显然是实验性的)ebnf包。然而,即使是一个简短的程序也无法从golang.org导入(在第4行的导入处出错):

package main

import "fmt"
import "golang.org/x/exp/ebnf"

const g = `
Production  = name "=" [ Expression ] "." .
Expression  = Alternative { "|" Alternative } .
Alternative = Term { Term } .
Term        = name | token [ "…" token ] | Group | Option | Repetition .
Group       = "(" Expression ")" .
Option      = "[" Expression "]" .
Repetition  = "{" Expression "}" .`

func main() {
    fmt.Println(g)
}

是否有任何地方说明只有golang.org/src/中的基本包可以导入(如果是这样的话)?

我真的很想在playground中使用这个实验性的包,甚至是像currency这样的非实验性补充库。

英文:

I had trouble finding a list of what packages may be imported in the go playground at http://play.golang.org/. I was trying to use the (apparently experimental) package for ebnf. However even a short program won't import from golang.org (breaks on the import on line 4):

package main

import "fmt"
import "golang.org/x/exp/ebnf"

const g = `
Production  = name "=" [ Expression ] "." .
Expression  = Alternative { "|" Alternative } .
Alternative = Term { Term } .
Term        = name | token [ "…" token ] | Group | Option | Repetition .
Group       = "(" Expression ")" .
Option      = "[" Expression "]" .
Repetition  = "{" Expression "}" .`

func main() {
	fmt.Println(g)
}

Is it stated anywhere that only base packages in golang.org/src/ would import (if that's the case)?

I'd really like to play with this experimental package and even non-experimental supplemental libraries like currency in the playground.

答案1

得分: 6

Playground上的<kbd>About</kbd>按钮提供了一些提示:

> Playground可以使用大部分标准库,但也有一些例外。

所谓的标准库是指列在Packages页面的Standard library部分中的包。Other部分中列出的包不符合条件(这就是你尝试的golang.org/x/exp/ebnf包,它属于实验性和已弃用的包,列在Other类别下)。

如果你想了解更多关于Playground实现的信息,这是一个必读的链接:

The Go Blog: Inside the Go Playground

这是一个详尽的Playground测试,导入了所有标准库包,以展示它们至少可以被导入,但这并不意味着一切(甚至任何东西)都可以合理地使用它们。标准库中唯一一个导致编译错误的包是runtime/cgo;没有可构建的Go源文件的“包”由于显而易见的原因不包括在内(因为如果一个文件夹不包含至少一个可构建的Go源文件,它就不是一个包)。

这是Playground链接,你可以自己尝试。

package main

import (
	_ "archive/tar"
	_ "archive/zip"

	_ "bufio"
	_ "bytes"

	_ "compress/bzip2"
	_ "compress/flate"
	_ "compress/gzip"
	_ "compress/lzw"
	_ "compress/zlib"

	_ "container/heap"
	_ "container/list"
	_ "container/ring"

	_ "crypto"
	_ "crypto/aes"
	_ "crypto/cipher"
	_ "crypto/des"
	_ "crypto/dsa"
	_ "crypto/ecdsa"
	_ "crypto/elliptic"
	_ "crypto/hmac"
	_ "crypto/md5"
	_ "crypto/rand"
	_ "crypto/rc4"
	_ "crypto/rsa"
	_ "crypto/sha1"
	_ "crypto/sha256"
	_ "crypto/sha512"
	_ "crypto/subtle"
	_ "crypto/tls"
	_ "crypto/x509"
	_ "crypto/x509/pkix"

	_ "database/sql"
	_ "database/sql/driver"

	_ "debug/dwarf"
	_ "debug/elf"
	_ "debug/gosym"
	_ "debug/macho"
	_ "debug/pe"
	_ "debug/plan9obj"

	_ "encoding"
	_ "encoding/ascii85"
	_ "encoding/asn1"
	_ "encoding/base32"
	_ "encoding/base64"
	_ "encoding/binary"
	_ "encoding/csv"
	_ "encoding/gob"
	_ "encoding/hex"
	_ "encoding/json"
	_ "encoding/pem"
	_ "encoding/xml"

	_ "errors"
	_ "expvar"
	_ "flag"
	_ "fmt"

	_ "go/ast"
	_ "go/build"
	_ "go/constant"
	_ "go/doc"
	_ "go/format"
	_ "go/importer"
	_ "go/parser"
	_ "go/printer"
	_ "go/scanner"
	_ "go/token"
	_ "go/types"

	_ "hash"
	_ "hash/adler32"
	_ "hash/crc32"
	_ "hash/crc64"
	_ "hash/fnv"

	_ "html"
	_ "html/template"

	_ "image"
	_ "image/color"
	_ "image/color/palette"
	_ "image/draw"
	_ "image/gif"
	_ "image/jpeg"
	_ "image/png"

	_ "index/suffixarray"

	_ "io"
	_ "io/ioutil"

	_ "log"
	_ "log/syslog"

	_ "math"
	_ "math/big"
	_ "math/cmplx"
	_ "math/rand"

	_ "mime"
	_ "mime/multipart"
	_ "mime/quotedprintable"

	_ "net"
	_ "net/http"
	_ "net/http/cgi"
	_ "net/http/cookiejar"
	_ "net/http/fcgi"
	_ "net/http/httptest"
	_ "net/http/httputil"
	_ "net/http/pprof"
	_ "net/mail"
	_ "net/rpc"
	_ "net/rpc/jsonrpc"
	_ "net/smtp"
	_ "net/textproto"
	_ "net/url"

	_ "os"
	_ "os/exec"
	_ "os/signal"
	_ "os/user"

	_ "path"
	_ "path/filepath"

	_ "reflect"
	_ "regexp"
	_ "regexp/syntax"

	_ "runtime"
	// _ "runtime/cgo"  // ERROR: missing Go type information
						// for global symbol: .dynsym size 60
	_ "runtime/debug"
	_ "runtime/pprof"
	_ "runtime/race"
	_ "runtime/trace"

	_ "sort"
	_ "strconv"
	_ "strings"
	_ "sync"
	_ "sync/atomic"
	_ "syscall"

	_ "testing"
	_ "testing/iotest"
	_ "testing/quick"

	_ "text/scanner"
	_ "text/tabwriter"
	_ "text/template"
	_ "text/template/parse"

	_ "time"
	_ "unicode"
	_ "unicode/utf16"
	_ "unicode/utf8"
	_ "unsafe"
)

func main() {
	println("ok")
}
英文:

The <kbd>About</kbd> button on the Playground gives some hint:

> The playground can use most of the standard library, with some exceptions.

By the standard library the packages of the standard library are meant, which are listed on the Packages page, under the Standard library section. Packages listed under the Other section do not qualify (which is what you have tried - package golang.org/x/exp/ebnf falls under the experimental and deprecated packages listed under the Other category).

A must-read link if you want to know more about the Playground implementation:

The Go Blog: Inside the Go Playground

Here is an exhaustive playground test to import all the standard library packages to show they at least can be imported, but that doesn't mean everything (or even anything) can be reasonably used from them. The only package from the standard library that gives a compile error is runtime/cgo; "packages" without a buildable Go source file are not included for obvious reasons (because a folder is not a package if it does not contain at least one buildable Go source file).

Here's the Playground Link to try it yourself.

package main
import (
_ &quot;archive/tar&quot;
_ &quot;archive/zip&quot;
_ &quot;bufio&quot;
_ &quot;bytes&quot;
_ &quot;compress/bzip2&quot;
_ &quot;compress/flate&quot;
_ &quot;compress/gzip&quot;
_ &quot;compress/lzw&quot;
_ &quot;compress/zlib&quot;
_ &quot;container/heap&quot;
_ &quot;container/list&quot;
_ &quot;container/ring&quot;
_ &quot;crypto&quot;
_ &quot;crypto/aes&quot;
_ &quot;crypto/cipher&quot;
_ &quot;crypto/des&quot;
_ &quot;crypto/dsa&quot;
_ &quot;crypto/ecdsa&quot;
_ &quot;crypto/elliptic&quot;
_ &quot;crypto/hmac&quot;
_ &quot;crypto/md5&quot;
_ &quot;crypto/rand&quot;
_ &quot;crypto/rc4&quot;
_ &quot;crypto/rsa&quot;
_ &quot;crypto/sha1&quot;
_ &quot;crypto/sha256&quot;
_ &quot;crypto/sha512&quot;
_ &quot;crypto/subtle&quot;
_ &quot;crypto/tls&quot;
_ &quot;crypto/x509&quot;
_ &quot;crypto/x509/pkix&quot;
_ &quot;database/sql&quot;
_ &quot;database/sql/driver&quot;
_ &quot;debug/dwarf&quot;
_ &quot;debug/elf&quot;
_ &quot;debug/gosym&quot;
_ &quot;debug/macho&quot;
_ &quot;debug/pe&quot;
_ &quot;debug/plan9obj&quot;
_ &quot;encoding&quot;
_ &quot;encoding/ascii85&quot;
_ &quot;encoding/asn1&quot;
_ &quot;encoding/base32&quot;
_ &quot;encoding/base64&quot;
_ &quot;encoding/binary&quot;
_ &quot;encoding/csv&quot;
_ &quot;encoding/gob&quot;
_ &quot;encoding/hex&quot;
_ &quot;encoding/json&quot;
_ &quot;encoding/pem&quot;
_ &quot;encoding/xml&quot;
_ &quot;errors&quot;
_ &quot;expvar&quot;
_ &quot;flag&quot;
_ &quot;fmt&quot;
_ &quot;go/ast&quot;
_ &quot;go/build&quot;
_ &quot;go/constant&quot;
_ &quot;go/doc&quot;
_ &quot;go/format&quot;
_ &quot;go/importer&quot;
_ &quot;go/parser&quot;
_ &quot;go/printer&quot;
_ &quot;go/scanner&quot;
_ &quot;go/token&quot;
_ &quot;go/types&quot;
_ &quot;hash&quot;
_ &quot;hash/adler32&quot;
_ &quot;hash/crc32&quot;
_ &quot;hash/crc64&quot;
_ &quot;hash/fnv&quot;
_ &quot;html&quot;
_ &quot;html/template&quot;
_ &quot;image&quot;
_ &quot;image/color&quot;
_ &quot;image/color/palette&quot;
_ &quot;image/draw&quot;
_ &quot;image/gif&quot;
_ &quot;image/jpeg&quot;
_ &quot;image/png&quot;
_ &quot;index/suffixarray&quot;
_ &quot;io&quot;
_ &quot;io/ioutil&quot;
_ &quot;log&quot;
_ &quot;log/syslog&quot;
_ &quot;math&quot;
_ &quot;math/big&quot;
_ &quot;math/cmplx&quot;
_ &quot;math/rand&quot;
_ &quot;mime&quot;
_ &quot;mime/multipart&quot;
_ &quot;mime/quotedprintable&quot;
_ &quot;net&quot;
_ &quot;net/http&quot;
_ &quot;net/http/cgi&quot;
_ &quot;net/http/cookiejar&quot;
_ &quot;net/http/fcgi&quot;
_ &quot;net/http/httptest&quot;
_ &quot;net/http/httputil&quot;
_ &quot;net/http/pprof&quot;
_ &quot;net/mail&quot;
_ &quot;net/rpc&quot;
_ &quot;net/rpc/jsonrpc&quot;
_ &quot;net/smtp&quot;
_ &quot;net/textproto&quot;
_ &quot;net/url&quot;
_ &quot;os&quot;
_ &quot;os/exec&quot;
_ &quot;os/signal&quot;
_ &quot;os/user&quot;
_ &quot;path&quot;
_ &quot;path/filepath&quot;
_ &quot;reflect&quot;
_ &quot;regexp&quot;
_ &quot;regexp/syntax&quot;
_ &quot;runtime&quot;
// _ &quot;runtime/cgo&quot;  // ERROR: missing Go type information
// for global symbol: .dynsym size 60
_ &quot;runtime/debug&quot;
_ &quot;runtime/pprof&quot;
_ &quot;runtime/race&quot;
_ &quot;runtime/trace&quot;
_ &quot;sort&quot;
_ &quot;strconv&quot;
_ &quot;strings&quot;
_ &quot;sync&quot;
_ &quot;sync/atomic&quot;
_ &quot;syscall&quot;
_ &quot;testing&quot;
_ &quot;testing/iotest&quot;
_ &quot;testing/quick&quot;
_ &quot;text/scanner&quot;
_ &quot;text/tabwriter&quot;
_ &quot;text/template&quot;
_ &quot;text/template/parse&quot;
_ &quot;time&quot;
_ &quot;unicode&quot;
_ &quot;unicode/utf16&quot;
_ &quot;unicode/utf8&quot;
_ &quot;unsafe&quot;
)
func main() {
println(&quot;ok&quot;)
}

答案2

得分: 2

我在http://play.golang.org/上找到了一个可以导入的包的列表。2022年的版本可以在https://go.dev/play/上找到。

找到一个确切的列表将比你现在(2019年5月16日)更加困难。

  • 可以导入由GOPROXY引用的任何包。
  • 可以导入你自己的包(在"play.ground"命名空间中定义)。
    请参阅Brad Fitzpatrick的公告。

现在#golang playground支持多个文件。

Dmitri Shuralyov补充说:

这意味着你也可以有多个模块!而且go.mod文件现在也会被格式化。

package main

import (
	"fmt"

	"gopher.example/bar"
	"play.ground/foo"
)

func main() {
	fmt.Println(foo.Bar)
	fmt.Println()
	fmt.Println(bar.Baz)
	fmt.Println()
	fmt.Println("And go.mod files get formatted too. Try pressing the Format button!")
}
-- go.mod --
module      "play.ground"

 replace (

     "gopher.example/bar"    => ./bar
)

-- foo/foo.go --
package foo

const Bar = "The Go playground now has support for multiple files!"
-- bar/go.mod --
module gopher.example/bar
-- bar/bar.go --
package bar

const Baz = "Which means you can have multiple modules too!"
  • 2022年3月:可以选择最新的Go版本(参见golang/go问题33629):
    1.17、1.18或开发版本。
英文:

> I had trouble finding a list of what packages may be imported in the go playground at http://play.golang.org/. 2022: https://go.dev/play/

Finding an exact list will be all the more difficult than you now (May. 16th 2019)

> And now the #golang playground has support for multiple files: Example

Dmitri Shuralyov adds:

> Which means you can also have multiple modules! And go.mod files get formatted now too: Example

package main
import (
&quot;fmt&quot;
&quot;gopher.example/bar&quot;
&quot;play.ground/foo&quot;
)
func main() {
fmt.Println(foo.Bar)
fmt.Println()
fmt.Println(bar.Baz)
fmt.Println()
fmt.Println(&quot;And go.mod files get formatted too. Try pressing the Format button!&quot;)
}
-- go.mod --
module      &quot;play.ground&quot;
replace (
&quot;gopher.example/bar&quot;    =&gt; ./bar
)
-- foo/foo.go --
package foo
const Bar = &quot;The Go playground now has support for multiple files!&quot;
-- bar/go.mod --
module gopher.example/bar
-- bar/bar.go --
package bar
const Baz = &quot;Which means you can have multiple modules too!&quot;
  • March 2022: can select between the most recent Go versions (see golang/go issue 33629):
    1.17, 1.18 or dev version.

答案3

得分: 1

这似乎取决于启动 playground 的环境。源代码中相关的部分似乎是 compileAndRun 函数,特别是第 88 行:

cmd.Env = []string{"GOOS=nacl", "GOARCH=amd64p32", "GOPATH=" + os.Getenv("GOPATH")}

它从环境中获取 GOPATH。

除此之外,playground 的源代码没有明确的可导入包的白名单(或黑名单)。

需要注意的是,compileAndRun 函数没有用于 go get 包的代码,所以 GOPATH 中已有的包就是可用的。

浏览 Makefile 和 Dockerfile 并没有揭示在 http://play.golang.org 网站上采取的具体部署步骤,所以我们只能依赖 Markus W Mahlberg 指出的文档;即“标准库的子集”。

此外,你可以部署自己的 go playground,并给它选择任何你想要的 GOPATH 环境。

英文:

It appears to depend on the environment in which the playground is launched. The relevant code in the source seems to be the compileAndRun func, especially line 88:

cmd.Env = []string{&quot;GOOS=nacl&quot;, &quot;GOARCH=amd64p32&quot;, &quot;GOPATH=&quot; + os.Getenv(&quot;GOPATH&quot;)}

Which gets the GOPATH from the environment.

Other than that, the playground source does not have any explicit whitelist (or blacklist) of importable packages.

It's probably key to note that the compileAndRun func has no code to go get packages, so whatever is already in the GOPATH is what is available.

A perusal of the Makefile and Dockerfile does not reveal the specific deployment steps taken in the canonical http://play.golang.org site, so we simply have to rely on the documentation that Markus W Mahlberg pointed out; i.e. "a subset of the stdlib".

Also, you can deploy your own version of the go playground, and give it whatever GOPATH environment you choose.

huangapple
  • 本文由 发表于 2016年4月5日 01:39:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/36409302.html
匿名

发表评论

匿名网友

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

确定