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
Tag: Golang
Golang – Check if IPv4 is valid and resolve FQDN
This recipe demonstrates if string is a valid IPv4, and also to resolve a FQDN to IP address. This recipe uses os.Args to get the second argument as FQDN and call net.ResolveIPAddr() function to do a FQDN resolution to IP address. package main import ( "fmt" "log" "net" "os" ) func isIPv4Addr(ip string) bool { … Continue reading Golang – Check if IPv4 is valid and resolve FQDN
Golang – Download files from website
This recipe downloads all kinds of files from website, but there is no progress tracking, only download the file. package main import ( "io" "log" "net/http" "os" ) func downloadFile(filepath, url string) error { resp, err := http.Get(url) if err != nil { return err } defer resp.Body.Close() f, err := os.Create(filepath) if err != … Continue reading Golang – Download files from website
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
[Golang]variadic function
This is the first time I have heard of this function, this is the same as python's args for unknown number of positional arguments in a function.
[Golang]BMI
I am using fmt.Scanln() to read user's input, the reader stops at newline, on previous case I use fmt.Scanf() but it reads the first input and terminates. This is part of an exercise for my golang tutorial.
[Golang]iota identifier
Reference: https://github.com/golang/go/wiki/Iota iota is used to declare incremental constants, each constant block will start iota with 0, then each constant within the block will increment the iota. Iota resets back to 0 if it is declared on another constant block.
[Golang]Area of a circle
Another exercise on golang, experience in python helps me pick up golang rather quickly. calculate the area of a circle.
[Golang]Fahrenheit to Celcius conversion
This is an exercise of my golang course. The purpose is to convert Fahrenheit to Celcius
[Go]Execute commands in Linux
I have been doing a few HTB machines and used some tools for enumeration and a lot of those tools are made from Golang, I wished to create my own tool in Go and hence I am starting to learn about the language. Here is a code snippet on how to interact with OS commands. … Continue reading [Go]Execute commands in Linux