Context in go
context is a standard package.
Context is an interface in context package.
type Context interface {
Deadline() (deadline time.Time, ok bool)
Done() <-chan struct{}
Err() error
Value(key interface{}) interface{}
}
There are 4 methods in Context interface. Most of the times, we don’t define our own types which satisfy the interface (which off course we can do), but we mostly use the factory functions provided by the context package itself.
They are Background(), TODO(), WithCancel(), WithTimeout() etc. The only difference is WithDeadline sets the termination time point, whileWithTimeout sets the maximum running time.
Closure in go
closure is a function inside another function where you can reference variable defined in the outer function.
I have seen them useful in dealing with packages where a function expects an type func() as an argument where the func() doesn’t take any argument.
example from package github.com/cenkalti/backoff
signature of backoff.Retry is as below
backoff.Retry(o backoff.Operation, b backoff.Backoff)
where backoff.Operation is of type func() error
type Operation func() error
func main() {
func retryWrapper(imp string) func() error{
return func() {
funcThatCouldFail(imp)
retun nil
}
}
err := backoff.Retry(retryWrapper("hello world"), backoff.NewExponentialBackOff())
}
we can’t directly call funcThatCouldFail(imp) in backoff.Retry(). So, we create a wrapper which takes an argument and that argument is being used inside of the func that being returned.
defer statement in go
defer is one of the unique keywords in Go.
What does defer do?
As the name suggests, it defers something. It defers function calls. A deferred function gets invoked right after the surrounding function returns.
func outer() {
defer inner()
log.Println("outer got called")
}
func inner() {
log.Println("inner got called")
}
outer got called prints first then inner got called.
What the main use of defer statement?
It helps mainly with actions that need to happen but not immediately. For eg. closing a file that’s opened or closing a database connection.
Notes on concurreny in Golang
Concurrency
channels are like typed pipes, Where you can send something from one side and receive from other side. using channel direction operator <-. Depends on the direction of the variables around the operator, its either sending or receiving. x := <-c // receive from c c <- sum // send sum to c fmt.Println(“channel receive happening”, <-c) channels are reference type. similar to maps. So when we pass channel as an argument to a function or copy we are passing the reference.
Getting Started with JWT
Disclaimer: Whatever I write here is my learnings. So, don’t take these words as granted or reference. Also, please lmk if there are any errors.
I have often come across this term JWT. I knew the definition - Json Web Token, but honestly that’s all I knew for a veryyyy longgg time. Recently, I came across a problem where I have been told that JWT might be a good solution for it. That piqued my interest.
Setting up ghost in raspberry pi for free
This is part of my Today I Learnt series where I share whatever I am learning something new. After having issues with port forwarding in Xfinity I decided to look for alternative solutions. I have used Cloudflare tunnel (used to be called Argo Tunnel) in the past to expose websites running on my laptop to the Internet. So, I decided to try it out for Ghost blogging site which I am setting up for my dad.