Skip to main content

28 posts tagged with "Go"

Go Programming Language and Go Modules

View All Tags

How to Use an if Statement in Go

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Programs make decisions: warn when it's hot, stop when an order isn't paid. Go's if runs its block only when the condition is true, and else if and else handle the other cases. Go has no brackets around the condition, and the braces are always required.

How to Write a Variadic Function in Go

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A variadic function accepts any number of values for its last parameter: none, one or many. fmt.Println is one. You write your own by putting ... before the parameter's type, and inside the function the values arrive as a slice.

How to Format Output in Go with fmt.Printf

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

fmt.Printf prints a template with placeholders, called verbs, filled in by your values: %s for text, %d for whole numbers, %.2f for decimals with two places. Two more, %v and %T, print any value and its type, which makes them handy while debugging.

How to Exit a Go Program with an Exit Code

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Every program ends with an exit code: 0 when it succeeded, anything else when it didn't. A Go program sets its own with os.Exit. There's one surprise on the way: go run doesn't pass that code on, so you only see the real number from the built program.