fileprivate

package module
v0.0.0-...-505994a Latest Latest
Warning

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

Go to latest
Published: May 13, 2022 License: MIT Imports: 6 Imported by: 0

README

fileprivate

A Go linter that enforces more strict members access rules inside packages.

Installation

$ go install github.com/quasilyte/fileprivate/cmd/fileprivate@latest

How to use?

Run it over a package(s) you want to check.

# Check "dbconn" package:
$ fileprivate ./dbconn

# Check all packages reachable from this root:
$ fileprivate ./...

What exactly does it do?

It checks that unexported types unexported members are not accessed wildly inside a signle package.

There are two exceptions to this rule to keep things pragmatic:

  • If the usage occurs in the same file then it's OK
  • If type T member is accessed from a T method declaration from another file

This code will trigger a warning:

// file1.go

type data struct {
    name string
}
// file2.go

func f(d *date) string {
    return d.name // accessing data.name member outside of the suggested context
}

Let's fix it:

 type data struct {
-    name string
+    Name string 
 }

Fixed code example:

// file1.go

type data struct {
    Name string
}
// file2.go

func f(d *date) string {
    return d.Name
}

Note: since fileprivate reports unexported types, it's never a breaking change to rename a field or a method. All changes are package-local and are invisible to the package users.

Rationale

Go has no concepts of file-protected and/or type-private.

What benefits do we get:

  • When refactoring comes, it's easier to move those types in another package: we already made necessary APIs exported and regulated the ways that object is used inside its original package.
  • For big package that can't be split into parts that's the way to keep things sane and avoid unfortunate incapsulation violations.

Keep in mind that this tool is not suitable for every Go code base out there. I would also not recommend adding it to your CI pipeline unless every member of your project agrees with it.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name: "fileprivate",
	Doc:  docString,
	Run:  run,
}

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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