prig
goimports
prig | goimports | |
---|---|---|
2 | 47 | |
64 | 7,238 | |
- | 0.7% | |
0.0 | 9.8 | |
almost 2 years ago | 6 days ago | |
Go | Go | |
MIT License | BSD 3-clause "New" or "Revised" License |
Stars - the number of stars that a project has on GitHub. Growth - month over month growth in stars.
Activity is a relative number indicating how actively a project is being developed. Recent commits have higher weight than older ones.
For example, an activity of 9.0 indicates that a project is amongst the top 10% of the most actively developed projects that we are tracking.
prig
-
I Don't Like Golang
My thoughts on his points, from someone who really likes Go and has used it heavily on small and large projects (1M LoC):
1. Probably a matter of taste, but I love this feature, just because of the lack of noisy public/private keywords everywhere that you see in Java et al. It also means you can tell from a usage (not just the definition) that something is exported, which is often useful.
As far as renaming goes, either rename the definition and see where the compiler complains, or get your IDE to do it (I use GoLand, but good things are said about gopls).
As for his example, the idiomatic way to write that is either just call it `usr` or `adminUser`, or use `user := &user{}` which is valid (if a little confusing).
2. This is a feature: it allows you to define interfaces only where you need them (on the consumer side), and you define the interface with only the methods you actually need. This means that when you add a bunch of new methods on the implementation, you don't need to change all the consumers. Go interfaces are amazing.
The downside he discusses almost never happens: it's surprising, but even in large projects I've never had structs accidentally implementing interfaces or a IsAdmin method being implemented with reversed polarity by accident.
3. Definitely has its downsides. Tooling helps find unchecked errors. Though I've found the biggest downside to explicit errors is the verbosity. You do get used to it, and the explicitness is at least clear.
4. There are a couple of "magical" things like this, but they're well known and documented, and simple to fix if you run into them. I love the fact I can just name a file foo_test.go and add TestFoo methods, and "go test" finds them automatically.
5. I have not found this to be the case, and in the rare cases it does happen, the compiler tells you loudly and it's easy to fix.
6. Yeah, this is a slight pain, but the semi-official "imports" package (golang.org/x/tools/imports) fixes it up, so you just run generated code through that (and it auto-formats the code as well). It's a couple of lines of code. See: https://github.com/benhoyt/prig/blob/2df1b65a2bdf34c10bb5e57...
7. Yeah, I wouldn't mind a ternary operator. Easily misused, which is why they didn't add it, but it would be really nice used judiciously, rather than the 4-line if-else block.
8. Fixed by sort.Slice, which avoids the need for Len and Swap (and even more so by the new generics "slices" package, coming soon). I guess this was added after the article was written?
9. Fixed by "Go modules", which is really well designed and works well (though opinions differ).
10. Fixed with generics being added in Go 1.18. And generic helpers like "slices" and "maps" packages coming soon.
11. Yeah, slightly annoying for newbies, though as he mentioned, tooling tells you. I do like the control you (can) get over allocation and memory management with Go slices.
As far as his summary goes (eg: the type system getting in your way for large programs), I have definitely not found that to be the case. The author doesn't like Go, and that's okay! I don't like Java. :-)
-
Difference in Script Speed
fwiw I remember ben hoyt making a awk2go thing, It was still in alpha though... he also had a go like language that took go and made it behave a lot like awk here it is... It's weird to switch to go but if you want things to run as fast as possible, You need all cores, I'd switch to either luajit (for single core CPUs) or go/rust. you can also try to parallelize with gnu parallel. and use mawk which is a JIT. that should be better.
goimports
-
Secure Randomness in Go 1.22
goimports has special-cased math/rand.Read vs crypto/rand.Read from basically the beginning. But https://github.com/golang/tools/commit/0835c735343e0d8e375f0... in 2016 references a time window where it could resolve "rand.Read" as "math/rand". Maybe you were in that time window?
- Gopls/v0.15.0
-
How to find all methods which return struct "Foo" (vscode or cli)
Just a guess, but it might be somewhere in gopls https://github.com/golang/tools/tree/master/gopls/doc On this page https://langserver.org/ it says it should support "finding references"
- Major rewrite of gopls released (2 weeks ago)
-
What LSP are y'all using?
Language server protocol. Here’s a good one: https://github.com/golang/tools/blob/master/gopls/README.md
-
Can Someone Explain To Me Like I'm 5
gopls was not able to find modules in your workspace.When outside of GOPATH, gopls needs to know which modules you are working on.You can fix this by opening your workspace to a folder inside a Go module, orby using a go.work file to specify multiple modules.See the documentation for more information on setting up your workspace:https://github.com/golang/tools/blob/master/gopls/doc/workspace.md.
- Latest gopls version still v0.11.0 from December 22?
- GitHub - orijtech/structslop: structslop is a static analyzer for Go that recommends struct field rearrangements to provide for maximum space/allocation efficiency.
-
betteralign - structs field alignment static analyzer for Go
For more gopls settings, you can see files in this folder: https://github.com/golang/tools/tree/master/gopls/doc
-
Linter for explicit hint to interface which gets implemented.
But finding which interface is satisfied by a type is trivial anyways through gopls which integrates conveniently into any LSP supporting IDE (such as VSCode and Goland).
What are some alternatives?
gofumpt - A stricter gofmt
nvim-lspconfig - Quickstart configs for Nvim LSP
goreturns - A gofmt/goimports-like tool for Go programmers that fills in Go return statements with zero values to match the func return types
GoLint - [mirror] This is a linter for Go source code. (deprecated)
staticcheck
golines - A golang formatter that fixes long lines
go-checkstyle - checkstyle for go
Go Metalinter
GoCover.io - GoCover.io offers the code coverage of any golang package as a service.
interfacer
apicompat - apicompat checks recent changes to a Go project for backwards incompatible changes
go-mod-outdated - Find outdated dependencies of your Go projects. go-mod-outdated provides a table view of the go list -u -m -json all command which lists all dependencies of a Go project and their available minor and patch updates. It also provides a way to filter indirect dependencies and dependencies without updates.