Can I import 3rd party package into golang playground

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

Can I import 3rd party package into golang playground

问题

我在谷歌上搜索了,但没有找到答案。这可能吗?如果是,怎么做?

Go Playground链接:https://play.golang.org/

英文:

I googled but got no answer. Is it possible? If yes, how to do it?

The Go Playground link: https://play.golang.org/

答案1

得分: 32

自2019年5月14日起,现在可以(来自Brad Fitzpatrick)!

> #golang playground现在支持第三方导入,通过https://proxy.golang.org/引入它们
>
> 示例:https://play.golang.org/p/eqEo7mqdS9l 🎉
>
> 多文件支持和其他一些功能即将推出。
> 在golang/go问题31944上报告错误,或在推特上报告。

(关于**“多文件”支持**,请参见自2019年5月16日以来的“在Go Playground中可以导入哪些包?”:在此处有一个示例

netbrain在评论中提供了另一个示例

在playground上:

package main

import (
    "fmt"

    "gonum.org/v1/gonum/mat"
)

func main() {
    v1 := mat.NewVecDense(4,[]float64{1,2,3,4})
    fmt.Println(mat.Dot(v1,v1))
}

使用mat.NewVecDense()创建列向量,并使用mat.Dot()返回v1v1的逐元素乘积的和,结果为'30'。

重点是:gonum/mat不是Go标准库的一部分。


原始答案:

关于Go Playground的最完整的文章是“Go Playground内部”,其中提到:

这些过程都不支持导入远程包(通过互联网访问)。它是一个非常自包含的系统(您可以在本地运行,也可以从play.golang.org使用),具有多个功能的存根或模拟,例如网络:

> 像文件系统一样,playground的网络堆栈是由syscall包实现的进程内伪造。它允许playground项目使用回环接口(127.0.0.1)。
对其他主机的请求将失败


2017年更新:

您有其他选择:

但它们仍然使用官方的Go Playground服务来构建和运行Go代码,因此仍不允许外部导入。

英文:

Since May 14th, 2019, it is now possible (from Brad Fitzpatrick)!

> The #golang playground now supports third-party imports, pulling them in via https://proxy.golang.org/
>
> Example: https://play.golang.org/p/eqEo7mqdS9l 🎉
>
> Multi-file support & few other things up next.
> Report bugs at golang/go issue 31944, or here on the tweeters.

(On the "multiple file" support, see, since May. 16th 2019, "Which packages may be imported in the go playground?": see an example here)

netbrain suggests in the comments another example:

On the playground:

package main

import (
    "fmt"

    "gonum.org/v1/gonum/mat"
)

func main() {
    v1 := mat.NewVecDense(4,[]float64{1,2,3,4})
    fmt.Println(mat.Dot(v1,v1))
}

woud give '30', using mat.NewVecDense() to create a column vector, and mat.Dot() to return the sum of the element-wise product of v1 and v1

The point being: gonum/mat is not part of the Go Standard Library.


Original answers:

The most complete article on Go Playground remains "Inside the Go Playground", which mentions:

None of those processes support importing a remote package (that would be accessed over the internet).
It is very much a self-contained system (that you can run locally as well as using it from play.golang.org), with multiple features stubbed or faked, like the network:

> Like the file system, the playground's network stack is an in-process fake implemented by the syscall package.
It permits playground projects to use the loopback interface (127.0.0.1).
Requests to other hosts will fail.


Update 2017:

You have alternatives:

But they still use use the official Go Playground service to build and run Go code, so that would still not allow for external imports.

答案2

得分: 1

我自己没有尝试过,但xiam/go-playground表明这是可能的:

导入自定义包

请记住,playground 用户无法安装或使用不属于 Go 标准库的包,如果您想展示一个特殊的包,您需要在沙盒或不安全沙盒的基础上创建一个稍微不同的 Docker 镜像...

(接着是如何通过修改 Dockerfile 来实现的示例)

这似乎表明编译是在自定义沙盒内进行的,就像 xiam/go-playground 项目中所示,因此这是可能的(不需要像 @VonC 的回答中所示的 play.golang.org)。

如果我有机会亲自测试,我会更新这个答案并提供更多细节。

英文:

I have not tried it myself but xiam/go-playground indicates that this is possible:

> Importing custom packages
>
> Remember that playground users won't be able to install or use packages that are not part of the Go standard library, in case you want to showcase a special package you'll have to create a slightly different docker image on top of the sandbox or the unsafebox...

(Followed by example of how to do this with a Dockerfile modification.)

This would seem to indicate that the compilation is (or at least can be) performed inside a custom sandbox as show in the xiam/go-playground project, thus making this possible (not requiring play.golang.org as indicated by @VonC's answer).

If I get a chance to test this myself I'll update this answer with more detail.

huangapple
  • 本文由 发表于 2015年1月7日 11:54:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/27812164.html
匿名

发表评论

匿名网友

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

确定