DevTips -- Go

moon indicating dark mode
sun indicating light mode
Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.

dump env vars

Sometimes you need to brute force it. To dump all environment variables from inside a @golang program, we can combine `range` and `os.Environ`

package main
import (
"os"
"fmt"
)
func main() {
for _, pair := range os.Environ() {
fmt.Println(pair)
}
}
LANG=en_US.UTF-8
PWD=/Users/christopherbiscardi/tmp
SHELL=/bin/zsh
TERM_PROGRAM_VERSION=3.2.9
TERM_PROGRAM=iTerm.app
COLORTERM=truecolor
COMMAND_MODE=unix2003
TERM=xterm-256color
HOME=/Users/christopherbiscardi
TMPDIR=/var/folders/br/5cvw25pn6zjbn5nqhf0ptnx80000gn/T/
PAGER=less
LESS=-R

range

\@golang doesn't have `.map`, but it does have for loop sugar called `range` which can be used to iterate over collections.

var nums = []int{10, 20, 30, 40, 50, 60, 70}
func main() {
for i, v := range nums {
fmt.Println(v)
}
}

Viper isSet

Viper (https://github.com/spf13/viper) lets you check to see if environment variables are set, so you can use that to early terminate a @golang program

for _, env := range []string{
"TWITTER_ACCESS_TOKEN",
"TWITTER_ACCESS_TOKEN_SECRET",
"TWITTER_CONSUMER_KEY",
"TWITTER_CONSUMER_SECRET",
}{
viper.BindEnv(env)
isSet := viper.IsSet(env)
if isSet == false {
panic(fmt.Sprintf("%v is not set", env))
}
}
access_token := viper.GetString("TWITTER_ACCESS_TOKEN")
access_token_secret := viper.GetString("TWITTER_ACCESS_TOKEN_SECRET")
consumer_key := viper.GetString("TWITTER_CONSUMER_KEY")
consumer_secret := viper.GetString("TWITTER_CONSUMER_SECRET")