Go Printing to Stdout and Stderr: Difference between revisions
Jump to navigation
Jump to search
Line 63: | Line 63: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
===Pointers=== | ===Pointers=== | ||
{{Internal|Pointers_in_Go#Displaying_Pointers|Pointers in Go | Displaying Pointers}} | |||
===Structs=== | ===Structs=== | ||
{{Internal|Go_Structs#Printing_Structs|Go Structs | Printing Structs}} | {{Internal|Go_Structs#Printing_Structs|Go Structs | Printing Structs}} |
Revision as of 23:54, 28 September 2023
Internal
Bootstrapping Functions
Both print()
and println()
are pre-declared functions, ready to use without any import.
TODO: https://golang.org/ref/spec#Bootstrapping
print()
println()
func main() {
println("something")
}
fmt Functions
fmt.Printf()
import "fmt"
// ...
fmt.Printf("something %s", "blue")
Print()
expects a format specifier (or a format string) as the first argument, which contains conversion characters (ex. %s
):
%v
%v
renders the value in a default format. When printing structs, the plus flag %+v
adds field names.
%#v
renders a Go-syntax representation of the value.
%T
%T
renders a Go-syntax representation of the type of the value.
Strings
color := "blue"
fmt.Printf("The color is %s\n", color)
Integers
size := 1
fmt.Printf("The size is %d\n", size)
Characters
c := 'x'
fmt.Printf("The character is %c\n", c)
Booleans
b := true
fmt.Printf("The boolean value is %t\n", b)
Floating Point Numbers
f := 1.0
fmt.Printf("The floating point value is %f\n", f)