table of Contents
description
The println() function is a built-in function of Go, which is used to output/print standard error. The println() function can print out the value of a string or variable.
Syntax and parameters
println(args ...)
name | meaning | Remarks |
args | parameter list | Parameters that can be omitted, the parameters can be any number. |
return value
The println function has no return value.
Usage example
1. Empty printing
When there are no parameters, the println() function just prints a newline character.
2. Print characters
The println function can print characters. It should be noted that println() converts the characters into int32 format and outputs them with a new line.
package main
func main() {
a := 'c'
println(a)
println('c', 'd')
}
operation result:
99
99 100
3. Print string
The println function can directly print a string.
package main
func main() {
demo := "I Love Go"
println("He said,", demo)
}
operation result:
He said, I Love Go
4. Printing integer
println() prints integer parameters to standard output and wraps them.
package main
func main() {
var demoInt32 int32 = 98
println(demoInt32)
var demoInt int = 99
println(demoInt)
var demoInt64 int64 = 100
println(demoInt64)
}
operation result:
98
99
100
5. Print floating point
println() prints floating-point parameters to standard output and wraps them. The output floating point numbers are in scientific notation format.
package main
func main() {
var demoFloat32 float32 = 98.0
println(demoFloat32)
var demoFloat64 float64 = 99.1
println(demoFloat64)
}
operation result:
+9.800000e+001
+9.910000e+001
6. Print structure
println() cannot print the structure type:
package main
type Student struct {
ID string
Name string
Age int
Score StudentScore
}
type StudentScore struct {
Chinese int
Math int
English int
}
func main() {
student := Student{
ID: "001",
Name: "mike",
Age: 10,
Score: StudentScore{
Chinese: 99,
Math: 100,
English: 100,
},
}
println("student:", student)
}
Run error:
# command-line-arguments
./txt4.go:28:9: illegal types for operand: print
Student
7. Print map
The println() function only prints out the address of the map, but not the contents of the map:
package main
func main() {
score := map[string]int{
"chinese": 117,
"math": 141,
"english": 104,
}
println("score:", score)
}
operation result:
score: 0xc000036688
8. Print the array
The println() function cannot print the array.
package main
func main() {
numbers := [3]int{300, 301, 302}
println(numbers)
}
operation result:
# command-line-arguments
./txt4.go:6:9: illegal types for operand: print
[3]int
9. Print slices
The println() function prints the len/cap value and address of the slice, the specific format is:
[len/cap] address
package main
func main() {
demo := []int{3, 6, 9}
println(demo)
slice := make([]string, 23)
println(slice)
}
operation result:
[3/3]0xc000036600
[23/23]0xc000036618
10. Print channel
Because the channel variable itself is a pointer, the println() function will print the address of the channel variable.
package main
func main() {
demo := make(chan int, 5)
demo <- 3
println(demo)
}
operation result:
0xc000066000
11. Printing function
println() prints the address of the function variable.
package main
import "strings"
func main() {
println(strings.Split)
}
operation result:
0x107e428