Jon Calhoun (@joncalhoun) 's Twitter Profile
Jon Calhoun

@joncalhoun

Teaching #golang & building fun projects • Want to learn Go with fun exercises & a web app? Try gophercises.com and usegolang.com

ID: 14943133

linkhttps://www.calhoun.io calendar_today29-05-2008 11:27:07

6,6K Tweet

9,9K Followers

392 Following

Jon Calhoun (@joncalhoun) 's Twitter Profile Photo

Go's defer statement queues code to run just before a function returns. It is a great way to clean up resources and avoid memory leaks. #golang

Go's defer statement queues code to run just before a function returns. It is a great way to clean up resources and avoid memory leaks. #golang
Jon Calhoun (@joncalhoun) 's Twitter Profile Photo

Hey GoHugo.io, do you have any plans to release a v1? Or any info on how long you intend to preserve backwards compatibility? I'm a fan and have been using it for a bit, but I don't love it when a new version introduces breaking changes. 😢

Jon Calhoun (@joncalhoun) 's Twitter Profile Photo

Which tech/coding blogs have your favorite design? Not content, but blogs that are aesthetically pleasing and make it enjoyable to read the technical content. Bonus points if you can point out what you love about each design.

Jon Calhoun (@joncalhoun) 's Twitter Profile Photo

Go's Context type can be used to create timeouts. This is handy for setting a time limit for any function that accepts a context. #golang Ex: We can cancel a web request if it is taking too long to complete ensuring that our code isn't waiting longer than we want.

Go's Context type can be used to create timeouts. This is handy for setting a time limit for any function that accepts a context. #golang

Ex: We can cancel a web request if it is taking too long to complete ensuring that our code isn't waiting longer than we want.
Jon Calhoun (@joncalhoun) 's Twitter Profile Photo

Go's HTTP server is concurrent by default. That's right, every incoming web request is handled in a goroutine without any extra code! Handling requests concurrently is great because it prevents a web server from blocking. For instance, if your handler needs to use an external

Jon Calhoun (@joncalhoun) 's Twitter Profile Photo

Be careful when reading HTTP request bodies with io.ReadAll. It is quick and easy, but if you are working with a large file it will be all be loaded into memory. Instead, consider reading chunks of the file, or streaming with a function like io.Copy. #golang

Be careful when reading HTTP request bodies with io.ReadAll. It is quick and easy, but if you are working with a large file it will be all be loaded into memory. Instead, consider reading chunks of the file, or streaming with a function like io.Copy. #golang
Jon Calhoun (@joncalhoun) 's Twitter Profile Photo

Embed files into you Go code with the "embed" package and the go:embed directive. This works for single files and directories. Super handy for including templates, version files, configs, and more. #golang

Embed files into you Go code with the "embed" package and the go:embed directive. This works for single files and directories. Super handy for including templates, version files, configs, and more. #golang
Jon Calhoun (@joncalhoun) 's Twitter Profile Photo

Consider using close() on a channel or using sync.WaitGroup to signal the completion of a goroutine. Both will... ✅ Work even if no receivers are reading from the channel. ✅ Allow multiple receivers to listen for completion #golang

Consider using close() on a channel or using sync.WaitGroup to signal the completion of a goroutine. Both will...

✅ Work even if no receivers are reading from the channel. 
✅ Allow multiple receivers to listen for completion

#golang
Jon Calhoun (@joncalhoun) 's Twitter Profile Photo

Consider returning cleanup functions when objects are created. This helps ensure that teardown isn't forgotten about and is easy to handle with Go's defer statement. #golang

Consider returning cleanup functions when objects are created. This helps ensure that teardown isn't forgotten about and is easy to handle with Go's defer statement.

#golang
Jon Calhoun (@joncalhoun) 's Twitter Profile Photo

With Go you can test your code without installing any extra libraries or tools by following these three steps: 1️⃣ Create a source file with _test.go as the suffix. Eg "sum_test.go" 2️⃣ Import the "testing" package & create a function with the signature TestXxxx(t *testing.T) 3️⃣

With Go you can test your code without installing any extra libraries or tools by following these three steps:

1️⃣ Create a source file with _test.go as the suffix. Eg "sum_test.go"
2️⃣ Import the "testing" package & create a function with the signature TestXxxx(t *testing.T)
3️⃣
Jon Calhoun (@joncalhoun) 's Twitter Profile Photo

The init() function in Go runs before main() or any tests. This makes it alluring to new developers for setting things up, but it has a few downsides: 1️⃣ It introduces hidden behavior. If someone imports a function with init in it, code runs without them ever realizing it and

Jon Calhoun (@joncalhoun) 's Twitter Profile Photo

Anyone have examples of when using init() was the right choice in their #golang code? I might not agree with all the examples, but I'm keen to see them!

Jon Calhoun (@joncalhoun) 's Twitter Profile Photo

Use sync.Once to initialize a value only once. For example, when loading in ENV variables, we can use sync.Once to ensure the variables are only loaded once, then read them from memory anytime we need them. #golang

Use sync.Once to initialize a value only once. For example, when loading in ENV variables, we can use sync.Once to ensure the variables are only loaded once, then read them from memory anytime we need them. #golang
Jon Calhoun (@joncalhoun) 's Twitter Profile Photo

In Go, every variable and import in your code must be used, otherwise the compile will throw an error. This helps keep compile times fast, but there are times when we may want to skip this behavior. In those cases the blank identifier (_) can be used as a placeholder. #golang

In Go, every variable and import in your code must be used, otherwise the compile will throw an error. This helps keep compile times fast, but there are times when we may want to skip this behavior. In those cases the blank identifier (_) can be used as a placeholder. #golang
Jon Calhoun (@joncalhoun) 's Twitter Profile Photo

Use errors.Is and errors.As to inspect errors. These functions work with wrapped errors to allow access to the underlying error. #golang

Use errors.Is and errors.As to inspect errors. These functions work with wrapped errors to allow access to the underlying error. #golang
Jon Calhoun (@joncalhoun) 's Twitter Profile Photo

Scalability is a red herring. - SQL can easily handle millions of users - Monolithic architecture is easier to build, deploy, and scale for a long time - Deploying your app in multiple regions with 100 users is bonkers - And the list goes on... Focus on building something

Jon Calhoun (@joncalhoun) 's Twitter Profile Photo

Use the empty for loop to create an infinite loop in Go. This can be helpful in a number of scenarios, such as continuing to do work until the app shuts down or you receive a signal to stop. #golang

Use the empty for loop to create an infinite loop in Go. This can be helpful in a number of scenarios, such as continuing to do work until the app shuts down or you receive a signal to stop. #golang