Here’s a common cheat sheet tool I swear by. It’s a curl service
called cht.sh. It helps you find the most relevant information
on a given task one wants to accomplish in a programming language
without having to switch to a browser and wind up getting side tracked while coding.
I use a zsh function shown below which allows me to call the service like a program with arguments, the first being the programming language or primary topic and the remaining words as a plain english query.
function cht() {
url="cht.sh/"
declare -i x=1
if [ "$#" -ge 1 ]
then
for n in $(seq 1 $#); do
url+=${(P)x}+
x=$(( $x + 1 ))
done
url=${url%?}
curl -s $url | less
fi
}
For example, cht ruby for loop returns:
array.each do |element|
element.do\_stuff
end
\# or
for element in array do
element.do\_stuff
end
\# If you need index, you can use this:
array.each\_with\_index do |element,index|
element.do\_stuff(index)
end
\# \[Eimantas\] \[so/q/2032875\] \[cc by-sa 3.0\]
Of course, it looks way better in the terminal formatted as glorious ansi.
Sometimes as a software engineer you have to pick up a new language or technology and already know the fundamentals, but don’t know how to do what should be a basic idiomatic task. Using this service as a quick reference is a far more efficient use of resources than consulting with a mentor, scouring the web, or going through chat gpt.
Mentorship is essential to improving and you should save them questions where a nuanced answer pertaining to your project or company will not exist online - system design and architectural pattern level things.
Just as another example, I was interested in learning about json web tokens for authenticating
microservices talking over grpc so cht go jwt gave me plenty of
helpful information. Here’s how
one can go about saving the answer as a local copy.
# Save to a file
cht go jwt > jwt.txt
# Read the file
less -f jwt.txt
/*
* To start, you need to import a JWT library in Golang (go get
* github.com/dgrijalva/jwt-go). You can find that library documentation
* in below link.
*
* https:github.com/dgrijalva/jwt-go
*
* Firstly, you need to create a token
*/
// Create the token
token := jwt.New(jwt.SigningMethodHS256)
// Set some claims
token.Claims["foo"] = "bar"
token.Claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
// Sign and get the complete encoded token as a string
tokenString, err := token.SignedString(mySigningKey)
// Secondly, parse that token
token, err := jwt.Parse(myToken, func(token *jwt.Token) (interface{}, error) {
// Don't forget to validate the alg is what you expect:
if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return myLookupKey(token.Header["kid"]), nil
})
if err == nil && token.Valid {
deliverGoodness("!")
} else {
deliverUtterRejection(":(")
}
/*
* Also, there are some examples for use JWT in GOlang like this \* https:github.com/slok/go-jwt-example
*
* EDIT-1
*/
package main
import (
"fmt"
"time"
"github.com/dgrijalva/jwt-go"
)
const (
mySigningKey = "WOW,MuchShibe,ToDogge"
)
func main() {
createdToken, err := ExampleNew([]byte(mySigningKey))
if err != nil {
fmt.Println("Creating token failed")
}
ExampleParse(createdToken, mySigningKey)
}
func ExampleNew(mySigningKey []byte) (string, error) {
// Create the token
token := jwt.New(jwt.SigningMethodHS256)
// Set some claims
token.Claims["foo"] = "bar"
token.Claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
// Sign and get the complete encoded token as a string
tokenString, err := token.SignedString(mySigningKey)
return tokenString, err
}
func ExampleParse(myToken string, myKey string) {
token, err := jwt.Parse(myToken, func(token *jwt.Token) (interface{}, error) {
return []byte(myKey), nil
})
if err == nil && token.Valid {
fmt.Println("Your token is valid. I like your style.")
} else {
fmt.Println("This token is terrible! I cannot accept this.")
}
}
// \[coditori\] \[so/q/36236109\] \[cc by-sa 3.0\]
Also, there’s basically an entry for any sys-admin-esque command or popular cli tool, so try giving it a shot and let me know how it goes.