[Go] Get the network interface for the external access

func GetExternalInterface() string { route := “” res := exec.Command(“ip”, []string{“route”}…) if out, err := res.Output(); err == nil { route = string(out) } routeData := strings.Split(strings.Split(route, “\n”)[0], ” “) for idx, word := range routeData { if word == “dev” { return routeData[idx+1] } } return “” }

[Go] Read all contents from a file

func ReadAllFromPath(path string, doStrip bool) (string, error) { if _, err := os.Stat(path); err != nil { return “”, err } data, err := ioutil.ReadFile(path) if err != nil { return “”, err } ret := string(data) if doStrip { ret = strings.TrimSpace(ret) } return ret, nil }

[Go] Get the current UTC datetime

const ( TimeFormUTC string = “2006-01-02T15:04:05.000000Z” ) func GetDateTimeNow() (int64, string) { utc := time.Now().UTC() ret := utc.Format(TimeFormUTC) return utc.Unix(), ret }

[Go] Check if an element exists in a list of structures

func ContainsElement(slice interface{}, element interface{}) bool { switch reflect.TypeOf(slice).Kind() { case reflect.Slice: s := reflect.ValueOf(slice) for i := 0; i < s.Len(); i++ { val := s.Index(i).Interface() if reflect.DeepEqual(val, element) { return true } } } return false }

[Go] Create an empty file (like touch)

package main import ( “log” “os” ) func main() { emptyFile, err := os.Create(“empty.txt”) if err != nil { log.Fatal(err) } emptyFile.Close() }

[Go] Create Directories (like mkdir -p)

package main import ( “log” “os” ) func main() { if err := os.MkdirAll(“path1/path2”, 0775); err != nil { log.Fatal(err) } }

Install Mail Server (iRedMail)

# Tested on Ubuntu 20.04 1. Configure Hostname $ hostnamectl set-hostname mail.example.com $ vi /etc/hosts 127.0.0.1 mail.example.com mail 2. Download iRedMail package $ wget https://github.com/iredmail/iRedMail/archive/1.4.0.tar.gz 3. Install iRedMail on Ubuntu $ tar xvfz 1.4.0.tar.gz $ cd iRedMail-1.4.0 $ chmod +x iRedMail.sh $ sudo ./iRedMail.sh [ Welcome and thanks for your use ] -> [ Default […]

Check TXT Records using nslookup

$ nslookup > set q=txt > [TXT domain]

Get Wildcard Certificate from Let’s Encrypt

$ sudo apt-get install letsencrypt certbot $ certbot certonly –manual -d *.example.com -d example.com –preferred-challenges dns

Programming Practice

https://programmers.co.kr/learn/challenges