Can I import 3rd party package into golang playground

10,550

Solution 1

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.

Solution 2

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.

Share:
10,550

Related videos on Youtube

Shuo
Author by

Shuo

Love to play, love to program.

Updated on September 14, 2022

Comments

  • Shuo
    Shuo over 1 year

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

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

    • mattn
      mattn over 9 years
      Currently, It's not possible.
    • VonC
      VonC almost 5 years
      Currently (May 2019, 4 years later)... it is possible! See my updated answer below.
  • VonC
    VonC almost 5 years
    @netbrain Thank you. I have included your example in the answer for more visibility.