Skip to main content

Golang Print Placeholders

·83 words·1 min·
Table of Contents
Lessons learned from `Learn Go with Tests` - This article is part of a series.
Part 2: This Article

A few sprintf placeholders that I’ve found handy coding in golang.

%q placeholder
#

%q placeholder formats strings

package main

import (
  "fmt"
)

func main() {
  fmt.Println(fmt.Sprintf("%q", "tralala"))
}
"tralala"

%t placeholder
#

The %t formats booleans

package main

import (
  "fmt"
)

func main() {
  fmt.Println(fmt.Sprintf("%t", true))
}
true

%#v placeholder
#

%#v is a nice way to echo structs

package main

import (
  "fmt"
)

type someStruct struct {
  someValue int
}

func main() {
  fmt.Println(fmt.Sprintf("%#v", someStruct{someValue: 3}))
}
main.someStruct{someValue:3}
Lessons learned from `Learn Go with Tests` - This article is part of a series.
Part 2: This Article