Recently started with Go,
and I’m not fully familiar with it yet.
What is going on under the hood when client hits an api endpoint?
Am I on the right track with the following: Request → Router → Context →
Sync pool → Goroutine with handler function →
Response → Dispose → GC
Can someone point me to the official or other reliable resources
where this topic is covered in detail, as I’m unable to find it?
Also, regarding database connections…
Official tutorial says:
“Making db a global variable simplifies this example.
In production, you’d avoid the global variable,
such as by passing the variable to functions that need it
or by wrapping it in a struct.”
I’ve seen this in most articles I found, but none explained why.
I mean, I’m going to follow official recommendation,
but what is the fundamental difference?
You can wrap it in struct, but both pointers point to the same
connection with internally managed connection pool.
I tested both ways with thousands of requests via Jmeter while monitoring
active and idle connections in database and got the same results.
Was using postgresql with sqlx which is a wrapper of standard db library.
So I’m a bit confused here.
What am I missing?
And lastly… @hollowaykeanho
I stumbled upon this.
Can you write a word or two about “package decoupling with vertical approach”
you mentioned there, if you don’t mind, of course.
Is that something like vertical slice, maybe?
The new documentation site has source codes at the bottom of the page. Feel free to explore in the future.
It’s more on the memory accessibility of the variable; not performance. It should not have any difference because they are both accessing a pointer.
In production system, you want more control over magic. A global variable that is accessible by any functions, inside or outside of package (depending on its access availability) so it can cause unwanted changes by anyone or everyone, especially messing around with a crucial component: your database access. What it meant is instead of:
var DB *sql.DB // let's make it worse by setting the variable to publicly accessible and func Rogue alters it in its own goroutine
For the latter, you have tighter control over your database memory object (like which function is responsible to overwrite/access the db memory object at a time). Also, at any given time, you know you can discard db safely (think concurrency as you scale).
Also you need to be aware that this actually works in Go so variable naming can be quite misleading within a function to mess things up:
Definitely not a data type. It’s just a strategy to package your codes in a way of reducing as many error checking as possible. Keep in mind that it’s just an opinion, not a rule.
To do that, you need to:
make use of internal/ package for development.
Be very good with pointers.
Always use struct type to structure package data up.
Good at ‘function as a value’.
The trick is:
Pass a logger struct object into the intermediate package (place inside your struct as an element, not global variable) for logging noisy error messages instead of returning all the way up to the app-level functions.
Make sure the logging function can handle optional logging because not everyone practice this strategy.
Make use of pointers as return value (Basically to use nil as a bad output). Then ask yourself: is the error message useful for your higher level function ‘customer’:
3.1. If ‘yes’, then you return error object (usually ‘no’. See )
3.2. Otherwise, just log it and return nil as value.
IMPORTANT: the intermediate package must be reusable for other app development so it will mature out from your internal/ package over time.