Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

[Go]goimports まとめ

Last updated at Posted at 2025-02-24

概要

サードパーティ製の CLI ツール。
指定した .go ファイルに対して、以下の操作をしてくれる。

  • import 行に不足しているパッケージを追加
  • import 行から使われていないパッケージを削除
  • import 行をグルーピングしつつ、グループ単位でアルファベット順にソート
  • gofmt のスタイルでフォーマット

インストール

go install golang.org/x/tools/cmd/goimports@latest

コマンド

goimports [flags] [path ...]

フラグ

-cpuprofile string

CPU プロファイルの出力

-d

ファイルを上書きする代わりに、差分を表示する。
削除される箇所が -, 追加される行が + で表示される。

例. 以下の main.go に対して goimports -d を実行

package main

import (
	"fmt"


	"time"
)

func main() {
	fmt.Println("Hello, World!")
}

func sample(ctx context.Context) {
	// TODO
}

出力結果:

diff -u main/main.go.orig main/main.go
--- main/main.go.orig   2025-02-24 13:48:44.000000000 +0900
+++ main/main.go        2025-02-24 13:48:44.000000000 +0900
@@ -1,10 +1,8 @@
 package main
 
 import (
+       "context"
        "fmt"
-
-
-       "time"
 )
 
 func main() {
y_fujinami@y-fujinamis-Mac moq_sample % goimports -d main/main.go
diff -u main/main.go.orig main/main.go
--- main/main.go.orig   2025-02-24 13:48:59.000000000 +0900
+++ main/main.go        2025-02-24 13:48:59.000000000 +0900
@@ -1,10 +1,8 @@
 package main
 
 import (
+       "context"
        "fmt"
-
-
-       "time"
 )
 
 func main() {
@@ -12,5 +10,5 @@
 }
 
 func sample(ctx context.Context) {
-  // TODO
+       // TODO
 }

-e

すべてのエラーを報告する(異なる行の最初の 10 個だけでなく)。

※実験してみたが、-e なしでも、エラー報告は特に打ち切られている様子もなくよくわからなかった

-format-only

true の場合、インポートを修正せずにフォーマットのみを行う。このモードでは、 goimports は実質 gofmt だが、インポートがセクションにグルーピングされる点が追加される。

-l

goimport のフォーマットと異なるファイルを一覧表示する。

-local string

この文字列で始まる imports を、サードパーティ製パッケージの後に配置する。; 区切りのリストとなる。

put imports beginning with this string after 3rd-party packages; comma-separated list

-memprofile string

メモリプロファイルの出力

-memrate int

0 より大きい値を指定した場合、runtime.MemProfileRate をセットする。

-srcdir dir

ディレクトリからのソースコードであるかのように、インポートを選択する。
単一のファイル上で操作する場合、dir は代わりに完全なファイル名になることがあります。

-trace string

トレースプロファイルの出力

-v

詳細なログ出力

-w

標準出力の代わりに、source ファイルへ結果を書き込む。

※逆に、このフラグをつけないと「整形後の結果が標準出力に表示されるのみ」となるため注意。

実験してみてわかったこと

グルーピングの挙動

1. 標準パッケージとサードパーティ製パッケージ(準標準パッケージ含む) で分けようとする

goimports

import (
	"context"
	"golang.org/x/text/number"
	"fmt"
	"go.opentelemetry.io/otel"
	"go.uber.org/zap"
)

goimports

import (
	"context"
	"fmt"

	"go.opentelemetry.io/otel"
	"go.uber.org/zap"
	"golang.org/x/text/number"
)

2.goimports 適用前に、パッケージ間に空行がある場合は、その空行がそもそもの明示的なグルーピングであると解釈され、そのグルーピングは保ちつつ、1. のグルーピングをする形になる

goimports

import (
	"go.uber.org/zap"
	"fmt"
	"golang.org/x/text/number"

	"go.opentelemetry.io/otel"
	"context"
)

goimports

import (
	"fmt"

	"go.uber.org/zap"
	"golang.org/x/text/number"

	"context"

	"go.opentelemetry.io/otel"
)

不明点

(TODO: ソース読まないとわからなそう)

-cpuprofile string
-memprofile string
-trace string
-memrate int
-local string
-srcdir dir

所感

元からグルーピングしちゃうとフォーマットが

参考

goimports (公式ドキュメント)

goimports (ソースコード)

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?