Golang Code Snippets

Get Current Working Directory

cwd, _ := syscall.Getwd()
log.Println("working directory:", cwd)

Get IP of a Domain

addr, err := net.LookupIP("abc.com")

Time Format

today := time.Now().Format("01-01-1970")

Access Data From a Cursor

cursor read

cursor, err := coll.Find(context.TODO(), bson.D{})
if err != nil {
	panic(err)
}

for cursor.Next(context.TODO()) {
	var result bson.D
	if err := cursor.Decode(&result); err != nil {
		log.Fatal(err)
	}
	fmt.Println(result)
}
if err := cursor.Err(); err != nil {
	log.Fatal(err)
}