GCI, alat yang mengontrol pesanan impor paket Go dan menjadikannya selalu deterministik.
Format keluaran yang diinginkan sangat dapat dikonfigurasi dan memungkinkan lebih banyak pemformatan khusus dibandingkan dengan goimport
.
GCI mempertimbangkan blok impor berdasarkan AST seperti di bawah ini:
Doc
Name Path Comment
Semua komentar akan tetap seperti semula, kecuali blok komentar yang terisolasi.
Blok komentar yang terisolasi seperti di bawah ini:
import (
"fmt"
// this line is isolated comment
// those lines belong to one
// isolated comment blocks
"github.com/daixiang0/gci"
)
GCI membagi semua blok impor menjadi beberapa bagian, sekarang mendukung enam jenis bagian:
Prioritasnya adalah standard > default > custom > blank > dot > alias > localmodule, semua bagian diurutkan berdasarkan abjad di dalamnya. Secara default, bagian kosong, titik, dan alias tidak digunakan, dan baris terkait dimasukkan ke grup lain.
Semua blok impor menggunakan satu TAB( t
) sebagai Indent.
Sejak v0.9.0, GCI selalu menempatkan blok impor C sebagai yang pertama.
Catatan :
nolint
sulit ditangani di tingkat bagian, GCI akan menganggapnya sebagai satu komentar.
Deteksi modul lokal dilakukan dengan membaca nama modul dari file go.mod
di direktori tempat gci
dipanggil . Artinya:
gci
dipanggil dari root modul (yaitu direktori yang berisi go.mod
)gci
dipanggil dari direktori yang berisi go.work
(walaupun mode ini akan berfungsi jika dipanggil dari dalam salah satu modul di ruang kerja) Untuk mengunduh dan menginstal versi rilis tertinggi yang tersedia -
go install github.com/daixiang0/gci@latest
Anda juga dapat menentukan versi tertentu, misalnya:
go install github.com/daixiang0/[email protected]
Kini GCI menyediakan dua metode baris perintah, terutama untuk kompatibilitas ke belakang.
GCI mendukung tiga mode operasi
Catatan
Sejak v0.10.0, tanda
-s
dan--section
hanya dapat digunakan beberapa kali untuk menentukan beberapa bagian. Misalnya, Anda dapat menggunakan-s standard,default
sebelumnya, tetapi sekarang Anda harus menggunakan-s standard -s default
. Perubahan yang dapat menyebabkan gangguan ini memungkinkan proyek untuk mendukung penentuan beberapa awalan khusus. (Silakan lihat di bawah.)
$ gci print -h
Print outputs the formatted file. If you want to apply the changes to a file use write instead !
Usage:
gci print path... [flags]
Aliases:
print, output
Flags:
--custom-order Enable custom order of sections
-d, --debug Enables debug output from the formatter
-h, --help help for print
-s, --section stringArray Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot > alias > localmodule. The default value is [standard,default].
standard - standard section that Go provides officially, like " fmt "
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
default - default section, contains all rest imports
blank - blank section, contains all blank imports.
dot - dot section, contains all dot imports. (default [standard,default])
alias - alias section, contains all alias imports.
localmodule: localmodule section, contains all imports from local packages
--skip-generated Skip generated files
--skip-vendor Skip files inside vendor directory
$ gci write -h
Write modifies the specified files in-place
Usage:
gci write path... [flags]
Aliases:
write, overwrite
Flags:
--custom-order Enable custom order of sections
-d, --debug Enables debug output from the formatter
-h, --help help for write
-s, --section stringArray Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot > alias > localmodule. The default value is [standard,default].
standard - standard section that Go provides officially, like " fmt "
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
default - default section, contains all rest imports
blank - blank section, contains all blank imports.
dot - dot section, contains all dot imports. (default [standard,default])
alias - alias section, contains all alias imports.
localmodule: localmodule section, contains all imports from local packages
--skip-generated Skip generated files
--skip-vendor Skip files inside vendor directory
$ gci list -h
Prints the filenames that need to be formatted. If you want to show the diff use diff instead, and if you want to apply the changes use write instead
Usage:
gci list path... [flags]
Flags:
--custom-order Enable custom order of sections
-d, --debug Enables debug output from the formatter
-h, --help help for list
-s, --section stringArray Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot > alias > localmodule. The default value is [standard,default].
standard - standard section that Go provides officially, like " fmt "
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
default - default section, contains all rest imports
blank - blank section, contains all blank imports.
dot - dot section, contains all dot imports. (default [standard,default])
alias - alias section, contains all alias imports.
localmodule: localmodule section, contains all imports from local packages
--skip-generated Skip generated files
--skip-vendor Skip files inside vendor directory
$ gci diff -h
Diff prints a patch in the style of the diff tool that contains the required changes to the file to make it adhere to the specified formatting.
Usage:
gci diff path... [flags]
Flags:
--custom-order Enable custom order of sections
-d, --debug Enables debug output from the formatter
-h, --help help for diff
-s, --section stringArray Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot > alias > localmodule. The default value is [standard,default].
standard - standard section that Go provides officially, like " fmt "
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
default - default section, contains all rest imports
blank - blank section, contains all blank imports.
dot - dot section, contains all dot imports. (default [standard,default])
alias - alias section, contains all alias imports.
localmodule: localmodule section, contains all imports from local packages
--skip-generated Skip generated files
--skip-vendor Skip files inside vendor directory
Gci enables automatic formatting of imports in a deterministic manner
If you want to integrate this as part of your CI take a look at golangci-lint.
Usage:
gci [-diff | -w rite] [--local localPackageURLs] path... [flags]
gci [command]
Available Commands:
completion Generate the autocompletion script for the specified shell
diff Prints a git style diff to STDOUT
help Help about any command
list Prints filenames that need to be formatted to STDOUT
print Outputs the formatted file to STDOUT
write Formats the specified files in-place
Flags:
-d, --diff display diffs instead of rewriting files
-h, --help help for gci
-l, --local strings put imports beginning with this string after 3rd-party packages, separate imports by comma
-v, --version version for gci
-w, --write write result to (source) file instead of stdout
Use " gci [command] --help " for more information about a command.
Catatan ::
Gaya lama hanya untuk pengujian lokal, tidak akan digunakan lagi, silakan gunakan gaya baru, golangci-lint
juga menggunakan gaya baru.
Jalankan gci write -s standard -s default -s "prefix(github.com/daixiang0/gci)" main.go
dan Anda akan menangani kasus berikut:
package main
import (
"golang.org/x/tools"
"fmt"
"github.com/daixiang0/gci"
)
ke
package main
import (
"fmt"
"golang.org/x/tools"
"github.com/daixiang0/gci"
)
package main
import (
"fmt"
go "github.com/golang"
"github.com/daixiang0/gci"
)
ke
package main
import (
"fmt"
go "github.com/golang"
"github.com/daixiang0/gci"
)
package main
import (
"fmt"
go "github.com/golang"
_ "github.com/golang/blank"
. "github.com/golang/dot"
"github.com/daixiang0/gci"
_ "github.com/daixiang0/gci/blank"
. "github.com/daixiang0/gci/dot"
)
ke
package main
import (
"fmt"
go "github.com/golang"
"github.com/daixiang0/gci"
_ "github.com/daixiang0/gci/blank"
_ "github.com/golang/blank"
. "github.com/daixiang0/gci/dot"
. "github.com/golang/dot"
)
package main
import (
testing "github.com/daixiang0/test"
"fmt"
g "github.com/golang"
"github.com/daixiang0/gci"
"github.com/daixiang0/gci/subtest"
)
ke
package main
import (
"fmt"
"github.com/daixiang0/gci"
"github.com/daixiang0/gci/subtest"
testing "github.com/daixiang0/test"
g "github.com/golang"
)
Dengan asumsi ini dijalankan di root repo ini (yaitu di mana github.com/daixiang0/gci
adalah modul lokal)
package main
import (
"os"
"github.com/daixiang0/gci/cmd/gci"
)
ke
package main
import (
"os"
"github.com/daixiang0/gci/cmd/gci"
)
Name
dan Path
di blok imporPath
dan Comment
di blok imporgoimports
terlebih dahulu lalu gunakan GCI)