Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/quasilyte/fileprivate
A Go linter that enforces more strict members access rules inside packages
https://github.com/quasilyte/fileprivate
codestructure codestyle conventions go golang incapsulation linter opinionated
Last synced: 21 days ago
JSON representation
A Go linter that enforces more strict members access rules inside packages
- Host: GitHub
- URL: https://github.com/quasilyte/fileprivate
- Owner: quasilyte
- License: mit
- Created: 2022-05-13T10:07:07.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-05-13T12:57:42.000Z (over 2 years ago)
- Last Synced: 2024-10-11T11:48:24.509Z (about 1 month ago)
- Topics: codestructure, codestyle, conventions, go, golang, incapsulation, linter, opinionated
- Language: Go
- Homepage:
- Size: 11.7 KB
- Stars: 9
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fileprivate
A Go linter that enforces more strict members access rules inside packages.
## Installation
```bash
$ go install github.com/quasilyte/fileprivate/cmd/fileprivate@latest
```## How to use?
Run it over a package(s) you want to check.
```bash
# 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 fileThis code will trigger a warning:
```go
// file1.gotype data struct {
name string
}
```
```go
// file2.gofunc f(d *date) string {
return d.name // accessing data.name member outside of the suggested context
}
```Let's fix it:
```diff
type data struct {
- name string
+ Name string
}
```Fixed code example:
```go
// file1.gotype data struct {
Name string
}
```
```go
// file2.gofunc 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.