HomeToolsAbout a20k

fmt

What is it

Formatted I/O with functions analogous to C's printf and scanf.

String Interpolation

import ( "fmt" ) name := "Sam" date := time.Now() // simple fmt.Sprintf("Hi, my name is %v", name) result := fmt.Sprintf("Hello, %v! Today is %v, it's %02v:%02v now.", name, date.Weekday(), date.Hour(), date.Minute())

Printing

Print Statement

fmt.Println is used for debugging purposes

import "fmt" fmt.Println("debug message")

Logging

log.Println is used for logging purposes

import "log" log.Println("debug message")

Log Levels

  • log.Fatal - logs the message and calls os.Exit(1)
  • log.Panic - logs the message and calls panic
  • log.Print - logs the message

Verbs

%v = value in a default format

  • %+v adds field names when printing struct

%s = string %d = base 10 integer %t = boolean word true or false

© VincentVanKoh