Phuong Le(@func25) 's Twitter Profileg
Phuong Le

@func25

A Go writer who has just moved to Twi... X, I write clickbaity content and am currently open to new opportunities.

ID:2782006646

linkhttp://blog.devtrovert.com calendar_today31-08-2014 10:00:39

514 Tweets

2,8K Followers

79 Following

Phuong Le(@func25) 's Twitter Profile Photo

💡Golang Tip #82: Enhance Debug Output with %+v and %q Instead of %v and %s.

When we're debugging, seeing clearer and more detailed output from your logs can be a huge help.

Take a common situation where you're quickly checking the state of a struct with fmt.Println:
Here, the…

💡Golang Tip #82: Enhance Debug Output with %+v and %q Instead of %v and %s. When we're debugging, seeing clearer and more detailed output from your logs can be a huge help. Take a common situation where you're quickly checking the state of a struct with fmt.Println: Here, the…
account_circle
Phuong Le(@func25) 's Twitter Profile Photo

💡 Golang Tip #81: To check if a string is empty, prefer s != '' over len(s) == 0.

Both solutions are perfectly fine and both techniques are commonly seen in Go's standard libraries.

> But what's the difference? Why should we prefer s != ''?
Using len(..) works for different…

💡 Golang Tip #81: To check if a string is empty, prefer s != '' over len(s) == 0. Both solutions are perfectly fine and both techniques are commonly seen in Go's standard libraries. > But what's the difference? Why should we prefer s != ''? Using len(..) works for different…
account_circle
Phuong Le(@func25) 's Twitter Profile Photo

This is such a damn good list, I just spent the whole day playing around with it.

collabnix.github.io/kubetools/

account_circle
Phuong Le(@func25) 's Twitter Profile Photo

💡 Golang Tip #80: Declare variables NEAR their usage

This is a common mistake among those who have been using Go for a little while.

People often put all the variables at the top of the function, and even worse, they use 'var':
While this might look neat or seem like a simple…

💡 Golang Tip #80: Declare variables NEAR their usage This is a common mistake among those who have been using Go for a little while. People often put all the variables at the top of the function, and even worse, they use 'var': While this might look neat or seem like a simple…
account_circle
Phuong Le(@func25) 's Twitter Profile Photo

💡 Golang Tip #79: If doing something unusual, comment why

I've been reviewing code for a while, and realized that one of the most common and frustrating issues is the lack of comments explaining unusual code choices.

When we've worked with Go long enough, we understand that…

💡 Golang Tip #79: If doing something unusual, comment why I've been reviewing code for a while, and realized that one of the most common and frustrating issues is the lack of comments explaining unusual code choices. When we've worked with Go long enough, we understand that…
account_circle
Phuong Le(@func25) 's Twitter Profile Photo

💡Golang Tip #78: Non-blocking channel send trick

When we send a value to a channel, we typically wait for the receiver to be ready to take the value:
But there are times when we don't want to wait.

For example, if you've followed the previous tip on using semaphore, we could…

💡Golang Tip #78: Non-blocking channel send trick When we send a value to a channel, we typically wait for the receiver to be ready to take the value: But there are times when we don't want to wait. For example, if you've followed the previous tip on using semaphore, we could…
account_circle
Phuong Le(@func25) 's Twitter Profile Photo

💡Golang Tip #77: Buffered channels as semaphores to limit goroutine execution

When we need to manage 'how many goroutines can access a resource simultaneously', using a semaphore is a reliable approach.

We can create a semaphore using a buffered channel, where the channel's…

💡Golang Tip #77: Buffered channels as semaphores to limit goroutine execution When we need to manage 'how many goroutines can access a resource simultaneously', using a semaphore is a reliable approach. We can create a semaphore using a buffered channel, where the channel's…
account_circle
Phuong Le(@func25) 's Twitter Profile Photo

💡 Golang Tip #76: Result forwarding in function calls.

When I first started using Go, I found one concept a bit tricky: result forwarding in function calls.

It's common to get multiple values back from a function, usually a result along with an error, look at how we handle the…

💡 Golang Tip #76: Result forwarding in function calls. When I first started using Go, I found one concept a bit tricky: result forwarding in function calls. It's common to get multiple values back from a function, usually a result along with an error, look at how we handle the…
account_circle
Phuong Le(@func25) 's Twitter Profile Photo

💡Golang Tip #75: Optimize multiple calls with singleflight.

Let's consider you've got a function that fetches data from the network or performs some I/O and it takes around 3 seconds:
The above function emits a different number after 10 seconds.

- Now, if you call this…

💡Golang Tip #75: Optimize multiple calls with singleflight. Let's consider you've got a function that fetches data from the network or performs some I/O and it takes around 3 seconds: The above function emits a different number after 10 seconds. - Now, if you call this…
account_circle
Phuong Le(@func25) 's Twitter Profile Photo

💡 Golang Tip #74: Make time.Duration clear and easy to understand

When you're coding with time intervals, you'll be using the time.Duration type a lot.

A common issue that can make things confusing is when you use seconds:
In Go, it's usually NOT the usual way to do it, but if…

💡 Golang Tip #74: Make time.Duration clear and easy to understand When you're coding with time intervals, you'll be using the time.Duration type a lot. A common issue that can make things confusing is when you use seconds: In Go, it's usually NOT the usual way to do it, but if…
account_circle
Phuong Le(@func25) 's Twitter Profile Photo

💡 Golang Tip #73: Implement String() for enum with the stringer tool.

Have you noticed when you print a duration in Go, like fmt.Println(time.Second), it shows up as '1s' instead of '1000000000' while time.Duration is int64.

This is because the time.Duration type has a…

💡 Golang Tip #73: Implement String() for enum with the stringer tool. Have you noticed when you print a duration in Go, like fmt.Println(time.Second), it shows up as '1s' instead of '1000000000' while time.Duration is int64. This is because the time.Duration type has a…
account_circle
Phuong Le(@func25) 's Twitter Profile Photo

💡Golang Tip #72: Case-Insensitive string comparison with strings.EqualFold.

When you need to compare strings without caring about whether the letters are upper or lower case, you might think to:

- Make both strings lower case using strings.ToLower()
- Or make them both upper…

💡Golang Tip #72: Case-Insensitive string comparison with strings.EqualFold. When you need to compare strings without caring about whether the letters are upper or lower case, you might think to: - Make both strings lower case using strings.ToLower() - Or make them both upper…
account_circle
Phuong Le(@func25) 's Twitter Profile Photo

💡 Golang Tip #71: sync.Pool, make it typed-safe with generics.

What is sync.Pool?

Before going to the typed-safe pool, for those who aren't familiar with sync.Pool, it's a Go standard library feature used to reuse objects.

This can reduce the number of memory allocations,…

💡 Golang Tip #71: sync.Pool, make it typed-safe with generics. What is sync.Pool? Before going to the typed-safe pool, for those who aren't familiar with sync.Pool, it's a Go standard library feature used to reuse objects. This can reduce the number of memory allocations,…
account_circle
Phuong Le(@func25) 's Twitter Profile Photo

💡Golang Tip #70: Implement a context-aware sleep function.

The regular time.Sleep() doesn't care about context. If you set it to pause for 5 minutes, it will pause the process for the full duration, no matter what.

Even if we try to use it with a context, like in the example…

💡Golang Tip #70: Implement a context-aware sleep function. The regular time.Sleep() doesn't care about context. If you set it to pause for 5 minutes, it will pause the process for the full duration, no matter what. Even if we try to use it with a context, like in the example…
account_circle
Phuong Le(@func25) 's Twitter Profile Photo

💡Golang Tip #69: Manage multiple goroutines with errgroup

When we're dealing with a bunch of goroutines, it can be a bit hard to handle errors and make sure they all work together well.

You might know sync.WaitGroup, right? But there's a package called errgroup that makes this…

💡Golang Tip #69: Manage multiple goroutines with errgroup When we're dealing with a bunch of goroutines, it can be a bit hard to handle errors and make sure they all work together well. You might know sync.WaitGroup, right? But there's a package called errgroup that makes this…
account_circle