Golang – Writing a command line program with urfave/cli package

The code recipe is modified from this video made by tutorialedge.net, this recipe does these: Help interface. To find out ipv4 address from fqdn: cli --host http://www.google.com ip Resolve FQDN to IPv4 addresses. To find out cname from given host: cli --host google.com cname Get the cname from given host. To find out mx records … Continue reading Golang – Writing a command line program with urfave/cli package

Golang – Encode Json data

This recipe demonstrates how to use struct to encode to json data. The struct type and its attributes have to be exportable in order for json encoding. The json data uses the attributes of struct type as keys if there is no customize key name defined. package main import ( "encoding/json" "fmt" "log" ) // … Continue reading Golang – Encode Json data

Golang – Find absolute path of a file.

This recipe finds the absolute path of the specified filename, the problem with this code is that it returns the first match and hence only useful if the filename is unique. package main import ( "fmt" "log" "os" "path/filepath" ) var absPath string // global variable to store absolute path of filename. func findFileAbsPath(basepath, filename … Continue reading Golang – Find absolute path of a file.

Golang – Obfuscate password input

This recipe shows how to obfuscate the password when user is typing the password. package p1 import ( "fmt" "log" "os" "golang.org/x/crypto/ssh/terminal" ) func getPassword() string { fmt.Println("\nPassword: ") // https://godoc.org/golang.org/x/crypto/ssh/terminal#ReadPassword // terminal.ReadPassword accepts file descriptor as argument, returns byte slice and error. passwd, e := terminal.ReadPassword(int(os.Stdin.Fd())) if e != nil { log.Fatal(e) } // … Continue reading Golang – Obfuscate password input

Golang – How to write ssh.HostKeyCallback

ssh.InsecureIgnoreHostKey is lazy and seems popular? I have seen many tutorials and some codes in github that ignore host key checking, this is not recommended as you need to ensure everytime you connect to the known ssh server is the actual server that serves your purpose, if host key checking is ignore then any server … Continue reading Golang – How to write ssh.HostKeyCallback