Prev Next

Golang / GoLang Basics Interview Questions

What are the most important formatting verbs in Go's fmt package?

Knowing fmt format verbs lets you produce clear output for debugging, logging, and user messages. The %v verb is the universal default; specialised verbs give more control.

Key fmt Format Verbs
VerbMeaningExample output
%vDefault format for any value42, true, [1 2 3]
%+vStruct with field names{Name:Alice Age:30}
%#vGo-syntax representationmain.Person{Name:"Alice", Age:30}
%TType of the valueint, []string, main.Person
%dInteger in decimal42
%f / %.2fFloat / float with 2 decimal places3.141590 / 3.14
%sPlain stringhello
%qQuoted string"hello"
%xHex encodingff (int) or 68656c6c6f (string)
%pPointer address0xc000012080
%wWrap an error (Errorf only)used for error chaining
name := "Alice"
age  := 30
pi   := 3.14159

fmt.Printf("%s is %d years old\n", name, age)  // Alice is 30 years old
fmt.Printf("Pi ≈ %.2f\n", pi)                  // Pi ≈ 3.14
fmt.Printf("Type: %T\n", pi)                   // Type: float64

// Sprintf — format to string, no output
msg := fmt.Sprintf("User: %s (age %d)", name, age)

// Errorf — format and create an error
err := fmt.Errorf("user %q not found (id: %d)", name, 99)

// Width and padding
fmt.Printf("%10s\n", "right")   //      right  (right-align, width 10)
fmt.Printf("%-10s|\n", "left")   // left      |  (left-align, width 10)
fmt.Printf("%06d\n", 42)         // 000042      (zero-pad to width 6)

// Printing structs
type Person struct{ Name string; Age int }
p := Person{"Bob", 25}
fmt.Printf("%v\n",  p)  // {Bob 25}
fmt.Printf("%+v\n", p)  // {Name:Bob Age:25}
fmt.Printf("%#v\n", p)  // main.Person{Name:"Bob", Age:25}
Which fmt verb prints a struct with its field names included?
What does fmt.Sprintf do?

Invest now in Acorns!!! 🚀 Join Acorns and get your $5 bonus!

Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!

Earn passively and while sleeping

Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.

Invest now!!! Get Free equity stock (US, UK only)!

Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.

The Robinhood app makes it easy to trade stocks, crypto and more.


Webull! Receive free stock by signing up using the link: Webull signup.

More Related questions...

What is Go and why was it created at Google? What are the key characteristics that make Go different from other popular languages? What are packages in Go and what is special about the 'main' package? What are the different ways to declare variables in Go? What are the fundamental data types in Go? How do constants and iota work in Go? How are functions defined in Go? What are variadic functions and named return values? How do if, for, and switch statements work in Go? What is the difference between arrays and slices in Go? How do maps work in Go? What are the key operations and pitfalls? How do structs work in Go and how do you attach methods to them? How do interfaces work in Go? How do you use type assertions and type switches? What is the empty interface (any / interface{}) and when should you use it? How do pointers work in Go and how are they safer than C pointers? How does Go handle errors, and what is the difference between %v and %w in fmt.Errorf? What are goroutines and how do you use sync.WaitGroup to wait for them? What are channels in Go and what is the difference between buffered and unbuffered? How do defer, panic, and recover work together in Go? What are closures in Go and what is the loop variable capture gotcha? What is the init() function and when does it run? What is the Go module system? What do go.mod and go.sum contain? What is a data race in Go and how do you detect one? What is the fmt.Stringer interface and how does it control how a type is printed? What is the difference between a type definition and a type alias in Go? How does Go handle strings, runes, and bytes? Why is len(s) not the character count? What is a goroutine leak and what is the idiomatic way to prevent one? What is sync.Mutex and when do you use sync.RWMutex instead? How does struct embedding promote methods in Go, and how does it differ from inheritance? How does append() work internally in Go? When does it allocate new memory? What is context.Context and why is it the first parameter in so many Go functions? What is the 'typed nil' trap in Go and why does 'if err != nil' sometimes fail? What are the most important formatting verbs in Go's fmt package?
Show more question and Answers...

Golang Internals and Memory Management Interview Questions

Comments & Discussions