Template Code in Playground

153 views
Skip to first unread message

Douglas Goddard

unread,
Jun 15, 2022, 12:09:16 PM6/15/22
to golan...@googlegroups.com
Hey, could the template code in the playground be changed from

import "fmt"

to

import (
    "fmt"
)

I find myself having to modify it to a multi line import every time I use it to test something out.

Thanks!

ben...@gmail.com

unread,
Jun 15, 2022, 7:17:39 PM6/15/22
to golang-dev
I second this. It's a very small thing, but slightly annoying to have to do.

I'd also be in favour of removing these comments, or at least moving them to a bit of UI text ... otherwise they're always there when you're writing playground examples, and I find myself removing them to avoid clutter whenever I "Share". Or maybe pressing "Share" would automatically remove those comments?

// You can edit this code!
// Click here and start typing.

-Ben

peterGo

unread,
Jun 16, 2022, 10:11:54 AM6/16/22
to golang-dev
Douglas,

I don't understand. The Go Playground uses goimports.

Command goimports updates your Go import lines, adding missing ones and removing unreferenced ones.  

https://pkg.go.dev/golang.org/x/tools/cmd/goimports

For example,

Go to the Go Playground template:

https://go.dev/play/

import "fmt"

Add a reference to the time package:

https://go.dev/play/p/nFpcPwbHOTK

import "fmt"

func main() {
    fmt.Println(time.Now())
}

Click the Run or Format command. The goimports command runs automatically:

https://go.dev/play/p/m8mx7s1-DiN

import (
    "fmt"
    "time"
)

func main() {
    fmt.Println(time.Now())
}

Peter

peterGo

unread,
Jun 16, 2022, 10:18:36 AM6/16/22
to golang-dev
Ben,

The UI comments embedded in the code are very annoying.

Peter

Josh Bleecher Snyder

unread,
Jun 16, 2022, 10:53:12 AM6/16/22
to peterGo, golang-dev
That doesn’t work for non-std imports. Those have to be added manually.

-josh


--
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-dev/e2e8b82e-e00a-4d43-80be-480a7e4e9c6cn%40googlegroups.com.

Sean Liao

unread,
Jun 16, 2022, 12:09:40 PM6/16/22
to golang-dev
the playground now always runs goimports, which also prefers the current import style for singular imports.
unless you're using third party packages, it should be rare to need to add imports manually.

- sean

--
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+...@googlegroups.com.

Mark Rushakoff

unread,
Jun 16, 2022, 12:09:40 PM6/16/22
to ben...@gmail.com, golang-dev
Both the run and format buttons update the import statement, at least for standard library packages.

Clicking Run or Format for this code will add the multiline import for "fmt" and "strconv":

package main

func main() {
  fmt.Println(strconv.Itoa(10))
}

Or if it already had the single-line import "fmt", it would update it to a multi-line import.


--

ka...@lemnisys.com

unread,
Jun 16, 2022, 1:43:37 PM6/16/22
to golang-dev
For non-std imports it can be made slightly easier by adding another import line and letting goimports turn it into a group.

import "fmt"
import "github.com/sirupsen/logrus"

will become

import (
    "fmt"

    "github.com/sirupsen/logrus"
)

ben...@gmail.com

unread,
Jun 16, 2022, 6:19:25 PM6/16/22
to golang-dev
> I don't understand. The Go Playground uses goimports.
> Command goimports updates your Go import lines, adding missing ones and removing unreferenced ones.

Ah, of course -- I didn't even realize! Here I was dutifully typing out all my imports before clicking Run. :-)

Though I'd still love to see the "UI comments" removed from the source.

-Ben

Reply all
Reply to author
Forward
0 new messages