dqlb38410 2019-08-06 14:25
浏览 68

多个语句比分解导入语句更好的情况? [关闭]

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?

  • 写回答

1条回答 默认 最新

  • donglian1384 2019-08-06 15:39
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看