LoginSignup
21
24

More than 3 years have passed since last update.

go mod tidy で不要なpackageを削除する

Last updated at Posted at 2019-08-28

概要

vgoでモジュール管理している際にgo getしたものの「やっぱり使わない!」から削除したいというケースだったり、「未使用のパッケージを削除したい」というケースだったりあると思います。

はい。go mod tidyでできます。(片仮名で発音を表すとタイディ)

go modコマンドを見てみる

$ go mod

Go mod provides access to operations on modules.

Note that support for modules is built into all the go commands,
not just 'go mod'. For example, day-to-day adding, removing, upgrading,
and downgrading of dependencies should be done using 'go get'.
See 'go help modules' for an overview of module functionality.

Usage:

    go mod <command> [arguments]

The commands are:

    download    download modules to local cache
    edit        edit go.mod from tools or scripts
    graph       print module requirement graph
    init        initialize new module in current directory
    tidy        add missing and remove unused modules
    vendor      make vendored copy of dependencies
    verify      verify dependencies have expected content
    why         explain why packages or modules are needed

Use "go help mod <command>" for more information about a command.

go mod tidyの説明にadd missing and remove unused modulesと書いてますね。
不要なパッケージの削除だけではなく、不足しているパッケージがあれば追加もしてくれるようです。

go help mod tidyしてみる

$ go help mod tidy
usage: go mod tidy [-v]

Tidy makes sure go.mod matches the source code in the module.
It adds any missing modules necessary to build the current module's
packages and dependencies, and it removes unused modules that
don't provide any relevant packages. It also adds any missing entries
to go.sum and removes any unnecessary ones.

The -v flag causes tidy to print information about removed modules
to standard error.

-vオプションをつけて削除されたパッケージ情報を出力するのがわかりやすい。

$ go mod tidy -v

unused github.com/ascarter/requestid
unused github.com/google/uuid
21
24
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
21
24