Go linter to check the struct literal to use field name

GoDoc

Structfield

Find struct literals using non-labeled fields.

The structfield analysis reports the usage of struct literal using non-labeled fields more than defined limit (default: 2). The non-labeled field above the limit considered has higher cognitive load (harder to understand and rememeber).

Understanding Struct Literal

Given code, variable assigned using struct literal:

acc := Account{
    "[email protected]",
    "John Smith",
    []Permission{
        Permission{"account", "read"},
        Permission{"account", "write"},
    },
    true,
    false,
}

Above code is harder to understand, hard to guess the field name since we have to remember exact order of the fields. The workaround is you have to always look the declaration of the Account type.

Suggestion is to refactor the code to:

acc := Account{
    Email: "[email protected]",
    Name: "John Smith",
    Permission: []Permission{
        Permission{"account", "read"}, // Non-labeled here is still ok
        Permission{"account", "write"},
    },
    Verified: true,
    Deactivated: false,
}

The limit set to 2, which considered easy to understand and remember.

Benefits

By using the labeled fields you several benefits

  1. The fields doesn't have to be in order
  2. You don't have to declare the value if it's a default value

Example:

acc := Account{
    Email: "[email protected]",
    Name: "John Smith",
    Permission: []Permission{
        Permission{"account", "read"}, // Non-labeled here is still ok
        Permission{"account", "write"},
    },
    Verified: true,
    Deactivated: false,
}

can be simplified into:

acc := Account{
    Name: "John Smith", // `Name` come first
    Email: "[email protected]",
    Permission: []Permission{
        Permission{"account", "read"}, // Non-labeled here is still ok
        Permission{"account", "write"},
    },
    Verified: true,
    // Remove the `Deactivated: false` since it use default value
}

Installation

$ go install github.com/uudashr/structfield/cmd/structfield@latest

or

$ go get github.com/uudashr/structfield/cmd/structfield

Usage

$ structfield -limit 2 testdata/src/a/*.go
testdata/src/a/a.go:20:9: Found 4 non-labeled fields on struct literal (> 2)
Owner
Nuruddin Ashr
I write code.. I read code.. Refactor.. Run checks.. Commit changes... `git push`
Nuruddin Ashr
Similar Resources

A full-featured license tool to check and fix license headers and resolve dependencies' licenses.

A full-featured license tool to check and fix license headers and resolve dependencies' licenses.

SkyWalking Eyes A full-featured license tool to check and fix license headers and resolve dependencies' licenses. Usage You can use License-Eye in Git

Dec 26, 2022

A tool to check problems about meta files of Unity

A tool to check problems about meta files of Unity

A tool to check problems about meta files of Unity on Git repositories, and also the tool can do limited autofix for meta files of auto-generated files.

Dec 22, 2022

Log4j check with golang

log4jcheck Install go install github.com/michael1026/log4jcheck@latest Example Usage cat URLs | log4jcheck -user-agent -referer -server example.burpc

Dec 11, 2021

A Runtime Struct Builder for Go

A Runtime Struct Builder for Go

Jul 8, 2022

gin struct controller

gin struct controller

Oct 4, 2021

Tugas Alta Immersive Backend Golang Fundamental Programming (Pointer, Struct, Method, Interface)

Tugas Alta Immersive Backend Golang Fundamental Programming (Pointer, Struct, Method, Interface)

Tatacara Melakukan Setup Tugas clone project ini dengan cara git clone https://github.com/Immersive-Backend-Resource/Pointer-Struct-Method-Interface.g

Jan 9, 2022

Highly configurable struct to map converter.

Mapify Highly configurable struct to map converter. Will convert maps into other maps as well (work in progress). Features configuration outside the s

Jul 30, 2022

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

Oct 24, 2022

Use Golang to implement PHP's common built-in functions.

PHP2Go Use Golang to implement PHP's common built-in functions. About 140+ functions have been implemented. Install go get github.com/syyongx/php2go R

Dec 28, 2022
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

Jan 8, 2023
Postgres uuid[] field for gorm.io - Golang

Postgres uuid[] field for gorm.io - Golang

Jan 17, 2022
gormuuid array field example

gormuuid - Examples An testing repo for https://github.com/ubgo/gormuuid module How to test Install the postgres and create a new database testdb Upda

Oct 20, 2021
Robust & Easy to use struct mapper and utility methods for Go

go-model Robust & Easy to use model mapper and utility methods for Go struct. Typical methods increase productivity and make Go development more fun ?

Dec 30, 2022
Go linter that warns about number of arguments in functions.

argslen linter Argslen is a linter that checks for long list of argument in functions. The default limit is 5 (maxArguments) and skip the test files (

Sep 17, 2022
A Golang tool to whitelist ASN's based on organization name

A Golang tool to whitelist ASN's based on organization name. This works by providing a list of ASN org names. This tool uses goPacket to monitor incoming traffic, capturing the IP's and checking the IP to see if it is a part of a whitelisted ASN. If it is not, it blocks that connection and future connections using iptables.

Jul 23, 2022
MCsniperGO, a fast, efficient, and feature-packed minecraft name sniper.

MCsniperGO This project was made possible by my donators Usage This sniper is in it's beta stage, meaning bugs should be expected. Easy installation d

Dec 31, 2022
A parser for Ontario's baby name data
A parser for Ontario's baby name data

obnp What? A parser for Ontario's baby name data Why? I wanted to see if a specific name existed in both the male and female datasets. This tool is mo

Mar 15, 2022
generate random data like name, email, uuid, address, images and etc.

gg-rand generate random data like name, email, uuid, address, images and etc. build and install: make run: gg-rand $ gg-rand SillyName : Knavesa

Nov 16, 2022
Calling functions by name and getting outputs by using reflect package.

Invoker A library to call (invoke) functions by taking names and sample inputs of those functions as parameters. And returns the types and values of o

Dec 20, 2021