💡Golang Tip #68: Use the deadcode tool to find and remove unused functions
We sometimes have code that isn't used, which we call "dead code".
It can make it harder for you to work on your code because we're not sure if you can remove it or change it.
Luckily, there's a tool called deadcode that can help we find this unused functions. We can install with this command:
I put it here so you can copy: go install https://t.co/VtQL1CF5Dl
And run it using this short command:
When we run it, it will show us the functions that aren't being used, with their location in your code.
If you want to know why a function is being used, we can use the -whylive flag, and the tool will tell us how the function is connected to the rest of our code.
From my experience, there may be a few times when the tool isn't right. So, use this tool to help you decide what code you can remove.
How it works (simplified)
1. The tool starts by reading all the code, checks the types.
2. It identifies the main() and any init() as the starting points.
3. From these starting points, deadcode looks at what functions are called directly. It makes a list of these functions because they are being used.
4. It then checks for functions that are called indirectly, through interfaces.
5. The tool keeps track of any types that are converted into interfaces because the methods of these types may be called indirectly.
6. Any function that isn't on this list after the tool has finished its analysis is considered "dead." This means the function isn't connected to the main path of running code.
----
You can read more about finding and removing dead code here: https://t.co/QvYKQu2CAW
#golang
The Pareto principle:
80% of the work can be finished within 20% of the time.
The remaining 20% take up the remaining 80% of the time.
Translated to software engineering:
Prefer solutions that are good enough over perfect solutions that take forever to finish.
Facebook uses the concept of adaptative LIFO for queues:
- They use FIFO order under normal conditions
- They switch over to LIFO order under high load
The rationale? By keeping FIFO order under high load, users may have already abandoned their request.
Therefore, processing the first-in request means spending resources on a request that is less likely to benefit the user than a request that has just arrived.
It keeps the fairness of FIFO and the practicality of LIFO. Fairly smart 👌
https://t.co/MS5r37lHKv
Here is a quick example of the new context.WithCancelCause introduced in #golang 1.21.
The output of the below program is "error from cancel func"
So why is this useful?
Previously if the context was canceled you either would not get an error back or you would get "Deadline Exceeded" but it could be tricky to figure out exactly which context span had errored or in what situation. This gives you more customisation and therefore clarity.
For me this is one of the nicest changes for readability since https://t.co/Y66AkKFns5 and https://t.co/qB7IYAJaW9 were introduced in go1.13
TIL (via @jhall_io) that any third party library can intercept all of your HTTP calls. I guess it's time to start using https://t.co/MSkaEuzXOy
#golang#security
No, it was not a joke. "Our paying customers need X, when will you fix it?" may not be the best way to introduce yourself to an open source project.
#TodayInOpenSource
@tebeka by thinking about it: we cannot make `R` as any?
That would be awesome! Just like "give me a pointer to any type". If anyone knows, I'd be interested!