在 go playground 中可以导入哪些包?

Which packages may be imported in the go playground?

我在 http://play.golang.org/. I was trying to use the (apparently experimental) package for ebnf 的 go playground 中找不到可以导入哪些包的列表时遇到了麻烦。然而,即使是一个短程序也不会从 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

这似乎取决于 playground 启动的环境。源代码中的相关代码似乎是 compileAndRun func,尤其是第 88 行:

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

从环境中获取 GOPATH。

除此之外,游乐场源没有任何可导入包的明确白名单(或黑名单)。

可能需要注意 compileAndRun 函数没有 go get 包的代码,因此 GOPATH 中已有的内容都是可用的。

细读 Makefile 和 Dockerfile 并没有揭示在规范 http://play.golang.org 站点中采取的具体部署步骤,因此我们只能依赖 Markus W Mahlberg 指出的文档;即 "a subset of the stdlib".

此外,您可以部署自己的 go playground 版本,并为其提供您选择的任何 GOPATH 环境。

Playground 上的关于 按钮给出了一些提示:

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

标准库是指标准库的包,它们列在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其他 类别下列出的实验性和弃用包。

如果您想了解有关 Playground 实现的更多信息,必读 link:

The Go Blog: Inside the Go Playground

这是一个详尽的游乐场测试,用于导入所有标准库包,以证明它们至少可以导入,但这并不意味着所有内容(甚至任何内容)都可以从他们合理使用。标准库中唯一给出编译错误的包是 runtime/cgo; "packages" 没有可构建的 Go 源文件不包括在内,原因很明显(因为一个文件夹是 not a package 如果它不包含至少一个可构建的 Go 源文件)。

这里是 Playground Link 自己尝试一下。

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")
}

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/

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

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 (
    "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 月:可以 select 在最新的 Go 版本之间 (参见 golang/go issue 33629):
    1.17、1.18 或开发版本。