### Install gocondense library Source: https://github.com/abemedia/gocondense/blob/master/README.md Use the go get command to add the package to your project dependencies. ```bash go get github.com/abemedia/gocondense@latest ``` -------------------------------- ### Install gocondense Source: https://github.com/abemedia/gocondense/blob/master/README.md Install the gocondense command-line tool using the Go toolchain. ```bash go install github.com/abemedia/gocondense/cmd/gocondense@latest ``` -------------------------------- ### Configure Vim formatting Source: https://github.com/abemedia/gocondense/blob/master/README.md Set up gocondense for Vim using either the vim-go plugin or a native autocommand. ```vim let g:go_fmt_command = "gocondense" ``` ```vim autocmd BufWritePre *.go silent execute '%!gocondense' ``` -------------------------------- ### Format source code Source: https://github.com/abemedia/gocondense/blob/master/README.md Use the library to format Go source code with either default or custom configuration settings. ```go formatted, err := gocondense.Source(src) ``` ```go f := gocondense.New(gocondense.Config{ MaxLen: 120, TabWidth: 2, }) formatted, err := f.Source(src) ``` -------------------------------- ### Configure Neovim with conform.nvim Source: https://github.com/abemedia/gocondense/blob/master/README.md Register gocondense as a formatter within the conform.nvim plugin configuration. ```lua require("conform").setup({ formatters_by_ft = { go = { "gocondense" }, }, formatters = { gocondense = { command = "gocondense", stdin = true, }, }, }) ``` -------------------------------- ### Run gocondense Source: https://github.com/abemedia/gocondense/blob/master/README.md Execute gocondense to format files in-place, process directories, or pipe content via stdin. ```bash gocondense file1.go file2.go # format files in-place gocondense ./ # format all .go files in a directory gocondense ./... # format all .go files recursively cat file.go | gocondense # read from stdin, write to stdout ``` -------------------------------- ### Configure VS Code settings Source: https://github.com/abemedia/gocondense/blob/master/README.md Add these settings to your VS Code configuration to use gocondense as the default Go formatter. ```json { "go.formatTool": "custom", "go.alternateTools": { "customFormatter": "gocondense" } } ``` -------------------------------- ### Collapse empty blocks Source: https://github.com/abemedia/gocondense/blob/master/README.md Empty function bodies, struct definitions, and interface definitions are collapsed onto a single line. ```go func noop() { } type S struct { } type I interface { } ``` ```go func noop() {} type S struct{} type I interface{} ``` -------------------------------- ### Trim leading and trailing blank lines Source: https://github.com/abemedia/gocondense/blob/master/README.md Removes leading and trailing blank lines from delimited constructs. ```go func foo() { bar() } ``` ```go func foo() { bar() } ``` ```go switch { case true: x := 1 } ``` ```go switch { case true: x := 1 } ``` ```go _ = map[string]int{ "a": 1, } ``` ```go _ = map[string]int{ "a": 1, } ``` -------------------------------- ### Condense function signatures Source: https://github.com/abemedia/gocondense/blob/master/README.md Condenses multi-line parameter, return, and type parameter lists onto single lines. Parts with comments are condensed independently. ```go func Add[ T ~int, ]( a T, b T, ) ( result T, err error, ) { return a + b, nil } ``` ```go func Add[T ~int](a, b T) (result T, err error) { return a + b, nil } ``` ```go func Partial[ T any, ]( a T, // keep b string, ) ( string, error, ) { return "", nil } ``` ```go func Partial[T any]( a T, // keep b string, ) (string, error) { return "", nil } ``` -------------------------------- ### Condense expressions Source: https://github.com/abemedia/gocondense/blob/master/README.md Condenses multi-line binary expressions, selector chains, and generic type instantiations. ```go _ = a + b _ = obj. Method _ = g[ int, string](1, "a") ``` ```go _ = a + b _ = obj.Method _ = g[int, string](1, "a") ``` ```go _ = a + // keep b* c + d ``` ```go _ = a + // keep b*c + d ``` -------------------------------- ### Unwrap single-item declaration groups Source: https://github.com/abemedia/gocondense/blob/master/README.md Unwraps single-item declaration groups onto a single line without parentheses. ```go import ( "fmt" ) const ( x = 1 ) var ( y = 2 ) type ( S struct{} ) ``` ```go import "fmt" const x = 1 var y = 2 type S struct{} ``` -------------------------------- ### Condense function calls Source: https://github.com/abemedia/gocondense/blob/master/README.md Condenses multi-line argument lists onto a single line. Calls are left untouched if any argument other than the last is multi-line. ```go result := myFunction( arg1, arg2, arg3, ) ``` ```go result := myFunction(arg1, arg2, arg3) ``` ```go processData( people, func(p Person) bool { return p.Age >= 18 }, ) ``` ```go processData(people, func(p Person) bool { return p.Age >= 18 }) ``` -------------------------------- ### Condense slice, array, and unkeyed struct literals Source: https://github.com/abemedia/gocondense/blob/master/README.md Condenses literals onto a single line if all elements are single-line. ```go numbers := []int{ 1, 2, 3, } ``` ```go numbers := []int{1, 2, 3} ``` -------------------------------- ### Remove unnecessary parentheses Source: https://github.com/abemedia/gocondense/blob/master/README.md Removes redundant parentheses while preserving those required for precedence. ```go x := (variable) y := (42) z := (string)("foo") unary := -(value) result := (a + b) * c // kept ``` ```go x := variable y := 42 z := string("foo") unary := -value result := (a + b) * c // kept ``` -------------------------------- ### Remove blank lines between assignment operators Source: https://github.com/abemedia/gocondense/blob/master/README.md Blank lines between assignment operators and values are removed, preserving comments. ```go a := 1 var b = // comment 2 ``` ```go a := 1 var b = // comment 2 ``` -------------------------------- ### Condense keyed struct and map literals Source: https://github.com/abemedia/gocondense/blob/master/README.md Condenses keyed literals only when the first element shares a line with the opening brace. ```go p := Person{Name: "John", Age: 30, } ``` ```go p := Person{Name: "John", Age: 30} ``` ```go p := Person{ Name: "John", Age: 30, } ``` -------------------------------- ### Group adjacent parameters with the same type Source: https://github.com/abemedia/gocondense/blob/master/README.md Merges adjacent type parameters or function parameters and results with the same type into a single declaration. ```go func foo(a int, b int) (c int, d int) { return a, b } type Pair[A any, B any] struct{} ``` ```go func foo(a, b int) (c, d int) { return a, b } type Pair[A, B any] struct{} ``` -------------------------------- ### Remove redundant len calls and zero bounds in slices Source: https://github.com/abemedia/gocondense/blob/master/README.md Redundant len calls and zero low bounds are removed from slice expressions, while complex expressions remain untouched. ```go _ = s[1:len(s)] _ = s[0:] ``` ```go _ = s[1:] _ = s[:] ``` ```go _ = s[0:2] // explicit non-zero high bound _ = s[0:len(s):cap(s)] // three-index slice _ = f()[0:len(f())] // side effects ``` -------------------------------- ### Remove blank identifiers in range statements Source: https://github.com/abemedia/gocondense/blob/master/README.md Unnecessary blank identifiers are removed from range statements. ```go for i, _ := range s { /* ... */ } ``` ```go for i := range s { /* ... */ } ``` ```go for _ = range s { /* ... */ } ``` ```go for range s { /* ... */ } ``` -------------------------------- ### Elide redundant types in composite literals Source: https://github.com/abemedia/gocondense/blob/master/README.md Redundant type names are removed from composite literals. ```go _ = []T{T{1, 2}, T{3, 4}} _ = []*T{&T{1, 2}, &T{3, 4}} _ = map[T]T{T{1, 2}: T{3, 4}} ``` ```go _ = []T{{1, 2}, {3, 4}} _ = []*T{{1, 2}, {3, 4}} _ = map[T]T{{1, 2}: {3, 4}} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.