Go tool to modify struct field tags

Related tags

go golang tool tags structs
Overview

gomodifytags

Go tool to modify/update field tags in structs. gomodifytags makes it easy to update, add or delete the tags in a struct field. You can easily add new tags, update existing tags (such as appending a new key, i.e: db, xml, etc..) or remove existing tags. It also allows you to add and remove tag options. It's intended to be used by an editor, but also has modes to run it from the terminal. Read the usage section below for more information.

gomodifytags

Install

go get github.com/fatih/gomodifytags

Supported editors

  • vim-go with :GoAddTags and :GoRemoveTags
  • go-plus (atom) with commands golang:add-tags and golang:remove-tags
  • vscode-go with commands Go: Add Tags and Go: Remove Tags
  • A (Acme) with commands addtags and rmtags
  • emacs-go-tag with commands go-tag-add and go-tag-remove

Usage

gomodifytags has multiple ways to modify a tag. Let's start with an example package:

package main

type Server struct {
	Name        string
	Port        int
	EnableLogs  bool
	BaseDomain  string
	Credentials struct {
		Username string
		Password string
	}
}

We have to first pass a file. For that we can use the -file flag:

$ gomodifytags -file demo.go
-line, -offset, -struct or -all is not passed

What are these? There are four different ways of defining which field tags to change:

  • -struct: This accepts the struct name. i.e: -struct Server. The name should be a valid type name. The -struct flag selects the whole struct, and thus it will operate on all fields.
  • -field: This accepts a field name. i.e: -field Address. Useful to select a certain field. The name should be a valid field name. The -struct flag is required.
  • -offset: This accepts a byte offset of the file. Useful for editors to pass the position under the cursor. i.e: -offset 548. The offset has to be inside a valid struct. The -offset selects the whole struct. If you need more granular option see -line
  • -line: This accepts a string that defines the line or lines of which fields should be changed. I.e: -line 4 or -line 5,8
  • -all: This is a boolean. The -all flag selects all structs of the given file.

Let's continue by using the -struct tag:

$ gomodifytags -file demo.go -struct Server
one of [-add-tags, -add-options, -remove-tags, -remove-options, -clear-tags, -clear-options] should be defined

Adding tags & options

There are many options on how you can change the struct. Let us start by adding tags. The following will add the json key to all fields. The value will be automatically inherited from the field name and transformed to snake_case:

$ gomodifytags -file demo.go -struct Server -add-tags json
package main

type Server struct {
	Name        string `json:"name"`
	Port        int    `json:"port"`
	EnableLogs  bool   `json:"enable_logs"`
	BaseDomain  string `json:"base_domain"`
	Credentials struct {
		Username string `json:"username"`
		Password string `json:"password"`
	} `json:"credentials"`
}

By default every change will be printed to stdout. So it's safe to run it and see the results of it. If you want to change it permanently, pass the -w (write) flag.

$ gomodifytags -file demo.go -struct Server -add-tags json -w

You can pass multiple keys to add tags. The following will add json and xml keys:

$ gomodifytags -file demo.go -struct Server -add-tags json,xml
package main

type Server struct {
	Name        string `json:"name" xml:"name"`
	Port        int    `json:"port" xml:"port"`
	EnableLogs  bool   `json:"enable_logs" xml:"enable_logs"`
	BaseDomain  string `json:"base_domain" xml:"base_domain"`
	Credentials struct {
		Username string `json:"username" xml:"username"`
		Password string `json:"password" xml:"password"`
	} `json:"credentials" xml:"credentials"`
}

If you prefer to use camelCase instead of snake_case for the values, you can use the -transform flag to define a different transformation rule. The following example uses the camelcase transformation rule:

$ gomodifytags -file demo.go -struct Server -add-tags json,xml -transform camelcase
package main

type Server struct {
	Name        string `json:"name" xml:"name"`
	Port        int    `json:"port" xml:"port"`
	EnableLogs  bool   `json:"enableLogs" xml:"enableLogs"`
	BaseDomain  string `json:"baseDomain" xml:"baseDomain"`
	Credentials struct {
		Username string `json:"username" xml:"username"`
		Password string `json:"password" xml:"password"`
	} `json:"credentials" xml:"credentials"`
}

Formatting tag values

By default a struct tag's value is transformed from a struct's field and used directly. As an example for the field Server string, we generate a tag in the form: json:"server" (assuming -add-tags=json is used).

However, some third party libraries use tags in a different way and might require to them to have a particular formatting, such as is the case of prefixing them (field_name=). The --template flag allows you to specify a custom format for the tag value to be applied.

$ gomodifytags -file demo.go -struct Server -add-tags gaum -template "field_name=$field" 
package main

type Server struct {
	Name        string `gaum:"field_name=name"`
	Port        int    `gaum:"field_name=port"`
	EnableLogs  bool   `gaum:"field_name=enableLogs"`
	BaseDomain  string `gaum:"field_name=baseDomain"`
}

The $field is a special keyword that is replaced by the struct tag's value after the transformation.

Transformations

We currently support the following transformations:

  • snakecase: "BaseDomain" -> "base_domain"
  • camelcase: "BaseDomain" -> "baseDomain"
  • lispcase: "BaseDomain" -> "base-domain"
  • pascalcase: "BaseDomain" -> "BaseDomain"
  • keep: keeps the original field name

You can also pass a static value for each fields. This is useful if you use Go packages that validates the struct fields or extract values for certain operations. The following example adds the json key, a validate key with the value set to gt=1 and the scope key with the value read-only:

$ gomodifytags -file demo.go -struct Server -add-tags json,validate:gt=1,scope:read-only
package main

type Server struct {
	Name        string `json:"name" validate:"gt=1" scope:"read-only"`
	Port        int    `json:"port" validate:"gt=1" scope:"read-only"`
	EnableLogs  bool   `json:"enable_logs" validate:"gt=1" scope:"read-only"`
	BaseDomain  string `json:"base_domain" validate:"gt=1" scope:"read-only"`
	Credentials struct {
		Username string `json:"username" validate:"gt=1" scope:"read-only"`
		Password string `json:"password" validate:"gt=1" scope:"read-only"`
	} `json:"credentials" validate:"gt=1" scope:"read-only"`
}

To add options to for a given key, we use the -add-options flag. In the example below we're going to add the json key and the omitempty option to all json keys:

$ gomodifytags -file demo.go -struct Server -add-tags json -add-options json=omitempty
package main

type Server struct {
	Name        string `json:"name,omitempty"`
	Port        int    `json:"port,omitempty"`
	EnableLogs  bool   `json:"enable_logs,omitempty"`
	BaseDomain  string `json:"base_domain,omitempty"`
	Credentials struct {
		Username string `json:"username,omitempty"`
		Password string `json:"password,omitempty"`
	} `json:"credentials,omitempty"`
}

If the key already exists you don't have to use -add-tags

Skipping unexported fields

By default all fields are processed. This main reason for this is to allow structs to evolve with time and be ready in case a field is exported in the future. However if you don't like this behavior, you can skip it by passing the --skip-unexported flag:

$ gomodifytags -file demo.go -struct Server -add-tags json --skip-unexported
package main

type Server struct {
        Name       string `json:"name"`
        Port       int    `json:"port"`
        enableLogs bool
        baseDomain string
}

Removing tags & options

Let's continue with removing tags. We're going to use the following simple package:

package main

type Server struct {
	Name        string `json:"name,omitempty" xml:"name,attr,cdata"`
	Port        int    `json:"port,omitempty" xml:"port,attr,cdata"`
	EnableLogs  bool   `json:"enable_logs,omitempty" xml:"enable_logs,attr,cdata"`
	BaseDomain  string `json:"base_domain,omitempty" xml:"base_domain,attr,cdata"`
	Credentials struct {
		Username string `json:"username,omitempty" xml:"username,attr,cdata"`
		Password string `json:"password,omitempty" xml:"password,attr,cdata"`
	} `json:"credentials,omitempty" xml:"credentials,attr,cdata"`
}

To remove the xml tags, we're going to use the -remove-tags flag:

$ gomodifytags -file demo.go -struct Server -remove-tags xml
package main

type Server struct {
	Name        string `json:"name"`
	Port        int    `json:"port"`
	EnableLogs  bool   `json:"enable_logs"`
	BaseDomain  string `json:"base_domain"`
	Credentials struct {
		Username string `json:"username"`
		Password string `json:"password"`
	} `json:"credentials"`
}

You can also remove multiple tags. The example below removs json and xml:

$ gomodifytags -file demo.go -struct Server -remove-tags json,xml
package main

type Server struct {
	Name        string
	Port        int
	EnableLogs  bool
	BaseDomain  string
	Credentials struct {
		Username string
		Password string
	}
}

If you want to remove all keys, we can also use the -clear-tags flag. This flag removes all tags and doesn't require to explicitly pass the key names:

$ gomodifytags -file demo.go -struct Server -clear-tags
package main

type Server struct {
	Name        string
	Port        int
	EnableLogs  bool
	BaseDomain  string
	Credentials struct {
		Username string
		Password string
	}
}

To remove any option, we can use the -remove-options flag. The following will remove all omitempty flags from the json key:

$ gomodifytags -file demo.go -struct Server -remove-options json=omitempty
package main

type Server struct {
	Name        string `json:"name" xml:"name,attr,cdata"`
	Port        int    `json:"port" xml:"port,attr,cdata"`
	EnableLogs  bool   `json:"enable_logs" xml:"enable_logs,attr,cdata"`
	BaseDomain  string `json:"base_domain" xml:"base_domain,attr,cdata"`
	Credentials struct {
		Username string `json:"username" xml:"username,attr,cdata"`
		Password string `json:"password" xml:"password,attr,cdata"`
	} `json:"credentials" xml:"credentials,attr,cdata"`
}

To remove multiple options from multiple tags just add another options:

$ gomodifytags -file demo.go -struct Server -remove-options json=omitempty,xml=cdata
package main

type Server struct {
	Name        string `json:"name" xml:"name,attr"`
	Port        int    `json:"port" xml:"port,attr"`
	EnableLogs  bool   `json:"enable_logs" xml:"enable_logs,attr"`
	BaseDomain  string `json:"base_domain" xml:"base_domain,attr"`
	Credentials struct {
		Username string `json:"username" xml:"username,attr"`
		Password string `json:"password" xml:"password,attr"`
	} `json:"credentials" xml:"credentials,attr"`
}

Lastly, to remove all options without explicitly defining the keys and names, we can use the -clear-options flag. The following example will remove all options for the given struct:

$ gomodifytags -file demo.go -struct Server -clear-options
package main

type Server struct {
	Name        string `json:"name" xml:"name"`
	Port        int    `json:"port" xml:"port"`
	EnableLogs  bool   `json:"enable_logs" xml:"enable_logs"`
	BaseDomain  string `json:"base_domain" xml:"base_domain"`
	Credentials struct {
		Username string `json:"username" xml:"username"`
		Password string `json:"password" xml:"password"`
	} `json:"credentials" xml:"credentials"`
}

Line based modification

So far all examples used the -struct flag. However we also can pass the line numbers to only change certain files. Suppose we only want to remove the tags for the Credentials struct (including the fields) for the following code (lines are included):

01  package main
02  
03  type Server struct {
04  	Name        string `json:"name" xml:"name"`
05  	Port        int    `json:"port" xml:"port"`
06  	EnableLogs  bool   `json:"enable_logs" xml:"enable_logs"`
07  	BaseDomain  string `json:"base_domain" xml:"base_domain"`
08  	Credentials struct {
09  		Username string `json:"username" xml:"username"`
10  		Password string `json:"password" xml:"password"`
11  	} `json:"credentials" xml:"credentials"`
12  }

To remove the tags for the credentials we're going to pass the -line flag:

$ gomodifytags -file demo.go -line 8,11 -clear-tags xml
package main

type Server struct {
	Name        string `json:"name" xml:"name"`
	Port        int    `json:"port" xml:"port"`
	EnableLogs  bool   `json:"enable_logs" xml:"enable_logs"`
	BaseDomain  string `json:"base_domain" xml:"base_domain"`
	Credentials struct {
		Username string
		Password string
	}
}

For removing the xml tags for certain lines, we can use the -remove-tags field. The following example will remove the xml tags for the lines 6 and 7 (fields with names of EnableLogs and BaseDomain):

$ gomodifytags -file demo.go -line 6,7 -remove-tags xml
package main

type Server struct {
	Name        string `json:"name" xml:"name"`
	Port        int    `json:"port" xml:"port"`
	EnableLogs  bool   `json:"enable_logs"`
	BaseDomain  string `json:"base_domain"`
	Credentials struct {
		Username string `json:"username" xml:"username"`
		Password string `json:"password" xml:"password"`
	} `json:"credentials" xml:"credentials"`
}

The same logic applies to adding tags or any other option as well. To add the bson tag to the lines between 5 and 7, we can use the following example:

$ gomodifytags -file demo.go -line 5,7 -add-tags bson
package main

type Server struct {
	Name        string `json:"name" xml:"name"`
	Port        int    `json:"port" xml:"port" bson:"port"`
	EnableLogs  bool   `json:"enable_logs" xml:"enable_logs" bson:"enable_logs"`
	BaseDomain  string `json:"base_domain" xml:"base_domain" bson:"base_domain"`
	Credentials struct {
		Username string `json:"username" xml:"username"`
		Password string `json:"password" xml:"password"`
	} `json:"credentials" xml:"credentials"`
}

Editor integration

Editors can use the tool by calling the tool and then either replace the buffer with the stdout or use the -w flag.

Also -line and -offset flags should be preferred to be used with editors. An editor can select a range of lines and then pass it to -line flag. The editor also can pass the offset under the cursor if it's inside the struct to -offset

Editors also can use the -format flag to output a json output with the changed lines. This is useful if you want to explicitly replace the buffer with the given lines. For the file below:

package main

type Server struct {
	Name        string
	Port        int
	EnableLogs  bool
	BaseDomain  string
	Credentials struct {
		Username string
		Password string
	}
}

If we add the xml tag and tell to output the format in json with the -format flag, the following will be printed:

$ gomodifytags -file demo.go -struct Server -add-tags xml -format json
{
  "start": 3,
  "end": 12,
  "lines": [
    "type Server struct {",
    "\tName        string `xml:\"name\"`",
    "\tPort        int    `xml:\"port\"`",
    "\tEnableLogs  bool   `xml:\"enable_logs\"`",
    "\tBaseDomain  string `xml:\"base_domain\"`",
    "\tCredentials struct {",
    "\t\tUsername string `xml:\"username\"`",
    "\t\tPassword string `xml:\"password\"`",
    "\t} `xml:\"credentials\"`",
    "}"
  ]
}

The output is defined with the following Go struct:

type output struct {
	Start int      `json:"start"`
	End   int      `json:"end"`
	Lines []string `json:"lines"`
}

The start and end specifies the positions in the file the lines will apply. With this information, you can replace the editor buffer by iterating over the lines and set it for the given range. An example how it's done in vim-go in Vimscript is:

let index = 0
for line_number in range(start, end)
  call setline(line_number, lines[index])
  let index += 1
endfor

Unsaved files

Editors can supply gomodifytags with the contents of unsaved buffers by using the -modified flag and writing an archive to stdin. Files in the archive will be preferred over those on disk.

Each archive entry consists of:

  • the file name, followed by a newline
  • the (decimal) file size, followed by a newline
  • the contents of the file

Development

At least Go v1.11.x is required. Older versions might work, but it's not recommended.

gomodifytags uses Go modules for dependency management. This means that you don't have to go get it into a GOPATH anymore. Checkout the repository:

git clone https://github.com/fatih/gomodifytags.git

Start developing the code. To build a binary, execute:

GO111MODULE=on go build -mod=vendor

This will create a gomodifytags binary in the current directory. To test the package, run the following:

GO111MODULE=on go test -v -mod=vendor

If everything works fine, feel free to open a pull request with your changes.

Issues
  • Cannot modify tags: invalid line directive found

    Cannot modify tags: invalid line directive found

    Committer: GitHub [email protected] 2019-07-16 15:06:38 is normal.

    Committer: Fatih Arslan [email protected] 2019-08-12 17:45:01 prompt:

    Cannot modify tags: invalid line directive found

    type BillStatQuery struct {
    	BuildingId string
    	SearchKey  string
    	Floor      int
    	RoomId     string
    }
    
    bug 
    opened by imkos 18
  • Wrong output when the file isn't gofmt'd

    Wrong output when the file isn't gofmt'd

    With the following file:

    $ cat tags.go
    package a
    type x struct {
        Foo int
        bar int
    }
    

    The tool gives the wrong results.

    Should be start=2:

    $ gomodifytags -format json -file tags.go -transform snakecase -line 3,4 -add-tags json -add-options json=
    {
      "start": 3,
      "end": 4,
      "lines": [
        "type x struct {",
        "\tFoo int `json:\"foo\"`"
      ]
    }
    

    Should be start=2, and one line is missing from the output:

    $ gomodifytags -format json -file tags.go -transform snakecase -line 3,3 -add-tags json -add-options json=
    {
      "start": 3,
      "end": 3,
      "lines": [
        "type x struct {"
      ]
    }
    

    start is correct, but the last line (}) missing:

    $ gomodifytags -format json -file tags.go -transform snakecase -offset 23 -add-tags json -add-options json=
    {
      "start": 2,
      "end": 5,
      "lines": [
        "",
        "type x struct {",
        "\tFoo int `json:\"foo\"`",
        "\tbar int `json:\"bar\"`"
      ]
    }
    

    It seems that the tool operates on the file as if they're gofmt'd.

    opened by arp242 7
  • Formatting tag values $field not generated

    Formatting tag values $field not generated

    I'm trying to generate with formatting tag values with -template. And I got null $field.

    script executed:

    gomodifytags -file purchase.go -all -add-tags gorm --template "column:$field" -w
    

    result:

    type PurchaseModel struct {
            BaseField         `json:",inline" gorm:"column:"`
            Code              string `json:"code,omitempty" gorm:"column:"`
            RefOrderId        string `json:"ref_order_id,omitempty" gorm:"column:"`
            Date              string `json:"date,omitempty" gorm:"column:"`
            Status            string `json:"status,omitempty" gorm:"column:"`
            PaymentStatus     string `json:"payment_status,omitempty" gorm:"column:"`
            InvoiceNumber     string `json:"invoice_number,omitempty" gorm:"column:"`
            InvoiceDate       string `json:"invoice_date,omitempty" gorm:"column:"`
            InvoiceDueDate    string `json:"invoice_due_date,omitempty" gorm:"column:"`
            InvoiceTotalPrice string `json:"invoice_total_price,omitempty" gorm:"column:"`
    }
    

    result gorm:"column:" should be gorm:"column:code"

    please help, Thanks

    opened by kecci 6
  • New Add/Remove prefix option

    New Add/Remove prefix option

    Some libraries like the gorm ORM or the gaum SQL query builder use a prefix in the tag name (ie gorm:"column:struct_field", gaum:"field_name=struct_field") to help ther Scanner/Valuer interact with the sql library in go. This PR adds the ability to add and remove said prefixes. I used these two libraries as samples in the tests files to help understand the rationale behind said feature to people reading without context, hopefully this doesn't break any project convention.

    opened by perrito666 5
  • Add new -transform option: keep

    Add new -transform option: keep

    To just keep the original values.

    opened by arp242 5
  • Support for lisp-case transforms

    Support for lisp-case transforms

    Hi,

    Would it be possible to add support for lisp-case, where the words are separated by hyphens?

    opened by urandom 5
  • anonymous structs?

    anonymous structs?

    Is there support for anonymous structs? gomodifytags doesn't seem to work on anonymous structs.

    Here's an example:

    Running gomodifytags -file main.go -line 7,9 -add-tags json returns selection is not inside a struct

    package main
    
    import "fmt"
    
    func main() {
    	anon := []struct {
    		ID int
    		A  string
    		B  string
    	}{}
    
    	fmt.Println(anon)
    }
    
    enhancement 
    opened by braindev 5
  • Feature Reqest: silent output

    Feature Reqest: silent output

    Currently even with -w flag the output still printed in stdout it would be nice if there's a flag that print only when there's error

    opened by kokizzu 5
  • Embedded struct can not be used with -transform camelcase

    Embedded struct can not be used with -transform camelcase

    type PDetail struct {
    	PSummary //will cause problem
    	Thumbnail             string `json:"thumbnail"`
    }
    
     W
    panic: runtime error: index out of range
    
    goroutine 1 [running]:
    main.(*config).addTags(0xc4200a4200, 0x0, 0x0, 0xc4200e74e0, 0x0, 0x100eda8, 0x20)
            /Users/ / /go/src/github.com/fatih/gomodifytags/main.go:351 +0x8a5
    
    opened by davyzhang 4
  • Different tags use different transforms

    Different tags use different transforms

    A tag on a struct uses the same transforms, and I want to use separate transforms for each tag.

    AppDeploymentServerModuleConfig struct {
    		ID                    int    `json:"id" gorm:"id"`
    		AppDeploymentConfigID int    `json:"appDeploymentConfigID" gorm:"app_deployment_config_id"`
    
    }
    

    json use camelcase transforms, gorm use snakecase transforms

    wontfix 
    opened by zhaogaolong 4
  • Feature Request: regex based modification

    Feature Request: regex based modification

    for example:

    gomodifytags bla.go -w -field-regex '.*Id' -add-options json=string
    gomodifytags bla.go -w -field-regex '.*At' -add-options json=string
    

    those will modify only fields that ends with Id or At

    opened by kokizzu 0
  • Cavebeavis/add printenvs

    Cavebeavis/add printenvs

    OVERVIEW

    So, have to say I love this tool. Thank you for your work on this! I am in the middle of a major refactor of about 50 services and this was the answer. I made a few modifications to fit my specific use case and some of this may be too specific for your tool but wanted to run these modifications by you in case you liked any of this. Feel free to ask for more general modification to fit your vision, or just say thanks but no thanks if you want -- no worries at all my friend.

    So the specifics are I am tooling our golang services to use https://github.com/ardanlabs/conf as our configuration loader and needed our config structures to support the env specific tags like conf:"env:IM_A_ENV". I also needed a simple way to print an env template for our ansible playbooks (company's choice).

    I felt it was important for the env to reflect the golang struct name first, then the property next.

    SPECIFIC REQUIREMENTS

    • Need support for environmental variable envcase like ENV_VARIABLE in order to refactor current non-docker services to support an .env file.
    • Need to be able to easily print the env template in the ENV_VARIABLE=variable format for the dev-ops team.

    ADDITIONAL

    • Updated go.mod to version 1.16
    • Updated go.mod dependencies
    • Updated README.md and tests where applicable.
    • A few minor refactoring -- like splitted to split, and realMain to a run function...
    • Updated README.md for the $field documentation to single quotes in lieu of double quotes (worked on Linux -- to be honest, did not test this on Windows or MAC).
    opened by cavebeavis 1
  •  Add external config file support to make editors configurable

    Add external config file support to make editors configurable

    Use external config file

    when we use editors like vscode to add tags, the vscode can only add json tags for us, there is no way to change the config, so I add a toml config support. we just need add a gomodifytags.toml in the same directory of gomodifytags. in linux we can use whereis gomodifytags to get the directory.

    Add = ["form", "gorm","binding"]
    Transform = "camelcase"
    [TemplateMap]
      gorm = "column:$field"
      binding = "required"
    [TransformMap]
      gorm = "snakecase"
    
    
    • the Add will add external tags to flag tag.
    • the Transform will config transform for all tag.
    • the TemplateMap will config template for each tag,overwrite the flag template.
    • the TransformMap will config transform for each tag,overwrite the flag transform and Transform.

    gomodifytags

    opened by kingeasternsun 0
Releases(v1.16.0)
  • v1.16.0(Sep 22, 2021)

    This release introduces a new titlecase transformation option. By passing the --transform titlecase flag, gomodifytags will transform the tags in the form of:

    "BaseDomain"` -> `"Base Domain"
    

    See related issue https://github.com/fatih/gomodifytags/issues/65 and the PR introducing this change: https://github.com/fatih/gomodifytags/pull/87

    Source code(tar.gz)
    Source code(zip)
  • v1.15.0(Sep 17, 2021)

    This release adds the new --quiet flag. When passed, gomodifytags won't output the results to stdout. This flag might be helpful for users who use gomodifytags for scripting in conjunction with the -w flag.

    Changes: https://github.com/fatih/gomodifytags/pull/86

    Source code(tar.gz)
    Source code(zip)
  • v1.14.0(Aug 25, 2021)

    This release changes the special keyword to format tag values from $field to {field}. It fixes the issue described in:

    https://github.com/fatih/gomodifytags/issues/76

    Changes included in this release are: https://github.com/fatih/gomodifytags/pull/85

    Source code(tar.gz)
    Source code(zip)
  • v1.13.0(Nov 4, 2020)

    This release includes the changes from the PR: https://github.com/fatih/gomodifytags/pull/64 gomodifytags handles now the following cases:

    • anonymous structs as function parameters
    func test(arg struct {
     	Field int `json:"field"`
     }) {
     }
    
    • "pointer to struct" type declaration
    var x *struct {
     	Field int `json:"field"`
     }
    
    • "array of struct" type declaration
    var x []struct {
     	Field int
     }
    

    and nested variants : "array of pointers to array of structs".

    Source code(tar.gz)
    Source code(zip)
  • v1.12.0(Oct 25, 2020)

    This release uses flag.CommandLine.Parse() instead of flag.Parse() to check for errors during flag parsing. This let us catch re-defined flag names.

    Related changes: https://github.com/fatih/gomodifytags/pull/75

    Source code(tar.gz)
    Source code(zip)
  • v1.11.0(Oct 22, 2020)

    The flag --format was already in use. v1.10.0 introduced a new feature that also provided the --format flag. This releases changes the flag to --template.

    PR: https://github.com/fatih/gomodifytags/pull/74

    Source code(tar.gz)
    Source code(zip)
  • v1.10.0(Oct 22, 2020)

    This release includes a new --format flag to change a struct tag's values based on the field name. This allows us to generate custom struct tags that don't follow the standard definition listed in https://golang.org/pkg/reflect/#StructTag. For a concrete example checkout the readme: https://github.com/fatih/gomodifytags#formatting-tag-values

    Includes the changes from PR: https://github.com/fatih/gomodifytags/pull/69

    Source code(tar.gz)
    Source code(zip)
  • v1.9.0(Oct 12, 2020)

  • v1.8.0(Oct 12, 2020)

  • v1.7.0(Oct 4, 2020)

    This release includes the new -field flag to modify a single field. It only works in conjunction with the -struct field and is a way to use gomodifytags from the terminal. Editors modify tags based on offset and are capable of editing single fields, hence they don't need this flag.

    PR: https://github.com/fatih/gomodifytags/pull/68

    Source code(tar.gz)
    Source code(zip)
  • v1.6.0(May 13, 2020)

    This releases fixes the case where gomodifytags would skip embedded unexported fields, even if -skip-unexported was enabled. Changes: https://github.com/fatih/gomodifytags/pull/62

    Source code(tar.gz)
    Source code(zip)
  • v1.5.0(May 11, 2020)

    Includes the new -all selection flag which can be used to select all structs in a given file. Changes: https://github.com/fatih/gomodifytags/pull/61

    Source code(tar.gz)
    Source code(zip)
  • v1.4.0(Apr 2, 2020)

  • v1.3.0(Feb 26, 2020)

  • v1.2.0(Jan 19, 2020)

    Includes the following change:

    • Rename the --skip-private flag to --skip-unexported to use the usual Go nomenclature https://github.com/fatih/gomodifytags/pull/54
    Source code(tar.gz)
    Source code(zip)
  • v1.1.0(Jan 17, 2020)

    A new --skip-private option has been added to skip private fields. By default it's disabled. https://github.com/fatih/gomodifytags/pull/53

    Source code(tar.gz)
    Source code(zip)
  • v1.0.1(Sep 4, 2019)

  • v1.0.0(Aug 6, 2019)

    gomodifytags is now in a good shape with a good feature set. Let's start tagging the release, which also sets the ground for future releases.

    Source code(tar.gz)
    Source code(zip)
Owner
Fatih Arslan
Software Engineer. Gopher and Coffee geek. Creator of vim-go. Tool maker.
Fatih Arslan
Faster golang reflection

In order to solve a problem that can work with any golang type, one has no choice but to use reflection. Native golang reflection comes with hefty performance price, on benchmarking simple getter/setter case to manipulate struct dynamically I've seen around 100 time worst performance comparing to statically typed code.

Viant, Inc 3 Aug 9, 2021
Copier for golang, copy value from struct to struct and more

Copier I am a copier, I copy everything from one to another Features Copy from field to field with same name Copy from method to field with same name

Jinzhu 2.2k Sep 22, 2021
Golang library to act on structure fields at runtime. Similar to Python getattr(), setattr(), hasattr() APIs.

go-attr Golang library to act on structure fields at runtime. Similar to Python getattr(), setattr(), hasattr() APIs. This package provides user frien

Shyamsunder Rathi 22 Aug 24, 2021
A library for diffing golang structures

Diff A library for diffing golang structures and values. Utilizing field tags and reflection, it is able to compare two structures of the same type an

R3 Labs 371 Sep 24, 2021
Helpfully Functional Go like underscore.js

/\ \ __ __ ___ \_\ \ __ _ __ ____ ___ ___ _ __ __ __ __

null 351 Sep 19, 2021
Jennifer is a code generator for Go

Jennifer Jennifer is a code generator for Go. package main import ( "fmt" . "github.com/dave/jennifer/jen" ) func main() { f := NewFile("m

Dave Brophy 2.2k Sep 22, 2021
Helpers for making the use of reflection easier

go-xray This is a Golang library with reflection related functions which I use in my different projects. KeyValue This type is used to construct a key

Pieter Claerhout 19 Sep 7, 2021
Go linter to check the struct literal to use field name

Structfield Find struct literals using non-labeled fields. The structfield analysis reports the usage of struct literal using non-labeled fields more

Nuruddin Ashr 3 Aug 23, 2021
Ento is an Entity Component System written in Go.

Ento is an Entity Component System written in Go.

Wojciech Franczyk 6 Sep 7, 2021
go-sundheit:A library built to provide support for defining service health for golang services

A library built to provide support for defining service health for golang services. It allows you to register async health checks for your dependencies and the service itself, and provides a health endpoint that exposes their status.

AppsFlyer 428 Sep 14, 2021
Code Generation for Functional Programming, Concurrency and Generics in Golang

goderive goderive derives mundane golang functions that you do not want to maintain and keeps them up to date. It does this by parsing your go code fo

Walter Schulze 929 Sep 22, 2021
Utilities to generate (reference) documentation for the docker CLI

About This is a library containing utilities to generate (reference) documentation for the docker CLI on docs.docker.com. Disclaimer This library is i

Docker 7 Sep 3, 2021
Code generation tools for Go.

interfaces Code generation tools for Go's interfaces. Tools available in this repository: cmd/interfacer cmd/structer cmd/interfacer Generates an inte

Rafal Jeczalik 307 Sep 10, 2021
.NET LINQ capabilities in Go

go-linq A powerful language integrated query (LINQ) library for Go. Written in vanilla Go, no dependencies! Complete lazy evaluation with iterator pat

Ahmet Alp Balkan 2.7k Sep 21, 2021
A tool and library for using structural regular expressions.

Structural Regular Expressions sregx is a package and tool for using structural regular expressions as described by Rob Pike (link).

Zachary Yedidia 23 Aug 23, 2021
Perforator is a tool for recording performance metrics over subregions of a program using the Linux "perf" interface.

Perforator Perforator is a tool for recording performance metrics over subregions of a program (e.g., functions) using the Linux "perf" interface.

Zachary Yedidia 22 Sep 6, 2021
Discover internet-wide misconfigurations while drinking coffee

netz ?? ?? The purpose of this project is to discover an internet-wide misconfiguration of network components like web-servers/databases/cache-service

null 293 Sep 12, 2021
A simple thread-safe, fixed size LRU written in Go. Based on dominictarr's Hashlru Algorithm. 🔃

go-hashlru A simple thread-safe, fixed size LRU written in Go. Based on dominictarr's Hashlru Algorithm. ?? Uses map[interface{}]interface{} to allow

Saurabh Pujari 67 Aug 17, 2021
go generate based graphql server library

gqlgen What is gqlgen? gqlgen is a Go library for building GraphQL servers without any fuss. gqlgen is based on a Schema first approach — You get to D

99designs 6.6k Sep 23, 2021