Print text and values with fmt.Println
Print a line
Use fmt.Println when you want to display values on their own line. Text in double quotes is a string literal, a literal sequence of characters. Numbers can be passed without quotes. Arguments are the values provided between the parentheses of the function call.
package main
import "fmt"
func main() { fmt.Println("Hello") fmt.Println("Go")}The output is Hello and then Go, on separate lines. Each call ends with a newline.
Print multiple arguments
package main
import "fmt"
func main() { fmt.Println("Total:", 5) fmt.Println(2 + 3)}Println inserts a space between the arguments and appends a newline. The addition expression is evaluated before being printed. In contrast, "2 + 3" is text, not an instruction to add. The function also returns a byte count and an error; this first exercise intentionally ignores them. Error handling is taught later.
Try it locally
Print Name: Ada on one line and Score: 7 on the next using two Println calls with two arguments each.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Getting Started 06: Print text and values with fmt.Println
Check the lesson objective before moving on.
What does fmt.Println("A", 3) print?
What is printed by fmt.Println("2 + 3")?