gosimports

package module
v0.3.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 11, 2023 License: BSD-3-Clause Imports: 4 Imported by: 3

README

Note APIs are likely to change a few times before we reach 1.0.0

gosimports - simpler goimports

  • 🚀 Drop-in replacement for goimports
  • 💯 Prettier than goimports
  • 🔨 Originally forked from golang.org/x/tools/cmd/goimports

Motivation

This gosimports provides one solution to the goimports grouping/ordering problem.

Installation

Go
go install github.com/rinchsan/gosimports/cmd/gosimports@latest
Homebrew
brew install rinchsan/tap/gosimports
Binary

Download binaries from GitHub Releases

Example

import (
	"bufio"

	// basic comments

	/*
		block comments
	*/

	"github.com/rinchsan/gosimports/internal/imports"

	"errors"
	gocmd "github.com/rinchsan/gosimports/internal/gocommand"
	"flag"

	"runtime"
	_ "runtime/pprof" // trailing inline comments
	"strings"

)

$ gosimports -w 👍

import (
	"bufio"
	"errors"
	"flag"
	"runtime"
	_ "runtime/pprof" // trailing inline comments
	"strings"

	gocmd "github.com/rinchsan/gosimports/internal/gocommand"
	"github.com/rinchsan/gosimports/internal/imports"
)

while goimports formatting like below 👎

import (
	"bufio"

	// basic comments

	/*
		block comments
	*/

	"github.com/rinchsan/gosimports/internal/imports"

	"errors"
	"flag"

	gocmd "github.com/rinchsan/gosimports/internal/gocommand"

	"runtime"
	_ "runtime/pprof" // trailing inline comments
	"strings"
)

Module Usage

Installation
go get github.com/rinchsan/gosimports
Example
package main

import (
	"os"

	"github.com/rinchsan/gosimports"
)

func main() {
	src, err := os.ReadFile("gosimports.go")
	if err != nil {
		// error handling
	}

	formatted, err := gosimports.Process("", src, nil)
	if err != nil {
		// error handling
	}

	if err := os.WriteFile("gosimports.go", formatted, 0644); err != nil {
		// error handling
	}
}

License

Copyright 2013 The Go Authors. All rights reserved.

Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Documentation

Overview

Package gosimports implements a Go pretty-printer that updates your Go import lines, adding missing ones, removing unreferenced ones, and removing redundant blank lines.

Index

Constants

This section is empty.

Variables

View Source
var Debug = false

Debug controls verbose logging.

View Source
var LocalPrefix string

LocalPrefix is a comma-separated string of import path prefixes, which, if set, instructs Process to sort the import paths with the given prefixes into another group after 3rd-party packages.

Functions

func Process

func Process(filename string, src []byte, opt *Options) ([]byte, error)

Process formats and adjusts imports for the provided file. If opt is nil, the defaults are used. If src is nil, the source is read from the filesystem.

Note that filename's directory influences which imports can be chosen, so it is important that filename be accurate. To process data “as if” it were in filename, pass the data as a non-nil src.

Types

type Options

type Options struct {
	Fragment  bool // Accept fragment of a source file (no package statement)
	AllErrors bool // Report all errors (not just the first 10 on different lines)

	Comments  bool // Print comments (true if nil *Options provided)
	TabIndent bool // Use tabs for indent (true if nil *Options provided)
	TabWidth  int  // Tab width (8 if nil *Options provided)

	FormatOnly bool // Disable the insertion and deletion of imports
}

Options specifies options for processing files.

Directories

Path Synopsis
cmd
gosimports
Command gosimports updates your Go import lines, adding missing ones, removing unreferenced ones, and removing redundant blank lines.
Command gosimports updates your Go import lines, adding missing ones, removing unreferenced ones, and removing redundant blank lines.
internal
event/core
Package core provides support for event based telemetry.
Package core provides support for event based telemetry.
event/export/eventtest
Package eventtest supports logging events to a test.
Package eventtest supports logging events to a test.
event/export/metric
Package metric aggregates events into metrics that can be exported.
Package metric aggregates events into metrics that can be exported.
event/export/ocagent
Package ocagent adds the ability to export all telemetry to an ocagent.
Package ocagent adds the ability to export all telemetry to an ocagent.
fastwalk
Package fastwalk provides a faster version of filepath.Walk for file system scanning tools.
Package fastwalk provides a faster version of filepath.Walk for file system scanning tools.
gocommand
Package gocommand is a helper for calling the go command.
Package gocommand is a helper for calling the go command.
gopathwalk
Package gopathwalk is like filepath.Walk but specialized for finding Go packages, particularly in $GOPATH and $GOROOT.
Package gopathwalk is like filepath.Walk but specialized for finding Go packages, particularly in $GOPATH and $GOROOT.
imports
Package imports implements a Go pretty-printer (like package "go/format") that also adds or removes import statements as necessary.
Package imports implements a Go pretty-printer (like package "go/format") that also adds or removes import statements as necessary.
proxydir
Package proxydir provides functions for writing module data to a directory in proxy format, so that it can be used as a module proxy by setting GOPROXY="file://<dir>".
Package proxydir provides functions for writing module data to a directory in proxy format, so that it can be used as a module proxy by setting GOPROXY="file://<dir>".
testenv
Package testenv contains helper functions for skipping tests based on which tools are present in the environment.
Package testenv contains helper functions for skipping tests based on which tools are present in the environment.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL