-4

The "A Tour of Go" guide says:

This code groups the imports into a parenthesized, "factored" import statement.

import (
    "fmt"
    "math"
)

You can also write multiple import statements, like:

import "fmt"
import "math"

But it is good style to use the factored import statement.

As I remember from the various Go talks I've watched, the Go programming language prides itself for being a simple, easy-to-use language that doesn't have 10 different ways to do one thing.

Is there any reason why there are multiple solutions to do the same thing? Are there any situations, where only the second approach works and the factored import statement is not able to solve a particular issue?

Vince Varga
  • 6,101
  • 6
  • 43
  • 60
  • 4
    *Are there any situations, where only the second approach works and the factored import statement is not able to solve a particular issue?* No, it's purely style. –  Aug 06 '19 at 14:26
  • There is no ambiguity here. Why do you say so? – sahaj Aug 06 '19 at 14:56
  • 1
    What is the *ambiguity* here? – Sarath Sadasivan Pillai Aug 06 '19 at 15:01
  • 1
    As Tim said it's entirely personal preference about which you choose. However, official Go docs highly recommend using the first syntax, and the go formatting tools (such as `gotfmt`) will reorganize your imports to use a single `import`. – Jessie Aug 06 '19 at 15:05
  • 1
    Go tooling will make sure you generally need not worry about this. – Emile Pels Aug 06 '19 at 15:06
  • 2
    In addition to `import`, the `var`, `const` and `type` declarations also have a parenthesized form. The `func` declaration is the only declaration that does not have a parenthesized form. @Jesse: The `gofmt` tool does not reorganize imports to use the parenthesized form (click format [here](https://tour.golang.org/basics/2) to see how gofmt handles multiple imports). – Charlie Tumahai Aug 06 '19 at 15:13
  • Right, there's no ambiguity as the tour pointed out to use the factored import, but I wanted to know whether there's a situation where only the "multiple import statements" solution would work. – Vince Varga Aug 06 '19 at 15:29
  • I wondered why Go has multiple solutions for the same problem ("import stuff"), therefore the question: in some languages, there's the case when only one way to doing things can solve a particular problem. – Vince Varga Aug 06 '19 at 15:31
  • 1
    I tried to clarify the question and the title... – Vince Varga Aug 06 '19 at 15:33
  • Multiple import statements have made it easier for me to generate code in the past (you don't have to worry about empty import lists), but I don't know if that's the rationale for allowing it. – Peter Aug 06 '19 at 19:16

1 Answers1

3

As pointed out by others, both forms can be used in all cases.

This grouping is allowed in almost all top-level identifiers (import, var, const and type), and its purpose is to give you the possibility to better organize and logically group your identifiers. (Note that you may also group inside a function not just at the top-level.)

With the case of import, tools like gofmt will order imports within a group primiarily by origin (standard lib or not), and secondary by import path alphabetically, but they don't reorganize between groups or the groups themselves.

In case of const it has another significance: the keyword const resets the iota predeclared identifier, so properly grouping constants when using iota is vital.

Other than that, it doesn't matter, you may use both forms. Group your identifiers based on their meaning and how they connect to each other. For example grouping a sync.Mutex with the variable it ought to protect is also handy:

var (
    mu        sync.Mutex
    protectMe int
)

func getMe() int {
    mu.Lock()
    me := protectMe
    mu.Unlock()
    return me
}

func setMe(me int) {
    mu.Lock()
    protectMe = me
    mu.Unlock()
}

(Example taken from How to make a variable thread-safe.)

As I remember from the various Go talks I've watched, the Go programming language prides itself for being a simple, easy-to-use language that doesn't have 10 different ways to do one thing.

Don't interpret this the wrong way. Both way of importing is the same, just formatted differently. Both specify the same set of imported packages. Just because a language allows you to add a and b in 2 ways, e.g. a+b and b+a, that doesn't mean a simple addition is allowed to be expressed in multiple ways.

icza
  • 389,944
  • 63
  • 907
  • 827