readonly

package module
v0.0.0-...-b9a7778 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 4 Imported by: 0

README

readonly

readonly is a Go linter for making exported fields read-only from outside the package.

Why

There are cases where we want to expose struct fields, but don't want them getting modified from outside the package.

A well-known solution is to make the fields unexported, and instead export getter functions that return them.

type ExampleStruct struct {
	someField string
}

func (e ExampleStruct) SomeField() string {
	return e.someField
}

With readonly we can simply export the struct fields, because readonly will report when they are being modified from outside the package.

// package a
type ExampleStruct struct {
	SomeField string
}

// package b
var example = ExampleStruct{
	SomeField: "some value",
}

example.SomeField = "new value" // Will report `readonly: field is being modified`.

You may have a look at the test files to get an idea of all the cases that readonly covers.

Installation

go install github.com/devinalvaro/readonly/cmd/readonly@latest

Usage

readonly [package]

readonly doesn't support additional flags at the moment.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAnalyzer

func NewAnalyzer() *analysis.Analyzer

Types

This section is empty.

Directories

Path Synopsis
cmd
internal

Jump to

Keyboard shortcuts

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