table of Contents
description
The fmt.Println function uses the default format of the parameter to write the parameter to standard output. In the case of multiple parameters, spaces will be added between the output of the parameters and a newline character will be appended. It returns the number of bytes written and any errors encountered.
Syntax and parameters
fmt.Println(args...)
name | meaning | Remarks |
args | parameter list | Parameters that can be omitted, the parameters can be any number. |
return value
The fmt.Println() function returns the number of bytes input to standard output and write errors.
return value | Description |
n | int type. Indicates the number of bytes written to standard output. |
err | Error type. Represents errors written to standard output. |
When writing to standard output is successful, err is null.
Usage example
Empty print
When there are no parameters, the fmt.Println() function just prints a newline character. At this time, the function returns 1, null.
package main
import "fmt"
func main() {
n, e := fmt.Println()
println(n)
println(e)
}
operation result:
1
(0x0,0x0)
Print characters
The fmt.Println function can print characters. It should be noted that fmt.Println() converts the characters into unicode codes and outputs them with a new line.
package main
import "fmt"
func main() {
n, e := fmt.Println('a')
println(n)
println(e)
}
operation result:
97
3
(0x0,0x0)
Print string
The fmt.Println function can directly print a string and wrap it.
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello Golang!")
fmt.Println("Hi, Kubernetes.")
}
operation result:
Hello Golang!
Hi, Kubernetes.
Print integer
fmt.Println() prints integer parameters to standard output and wraps them.
package main
import (
"fmt"
)
func main() {
n, _ := fmt.Println(23)
fmt.Println(n)
}
operation result:
23
3
Print float
fmt.Println()() prints floating-point parameters to standard output and wraps them.
package main
import (
"fmt"
)
func main() {
n, _ := fmt.Println(23.567)
fmt.Println(n)
}
operation result:
23.567
7
Print structure
package main
import "fmt"
type Employee struct {
Id string
Name string
Sex string
Age int
}
func main() {
employee := Employee{
Id: "NSB-EXT001",
Name: "Bell",
Sex: "man",
Age: 25,
}
fmt.Println(employee)
}
The fmt.Println() function prints out the contents of the structure:
{NSB-EXT001 Bell man 25}
Print map
package main
import "fmt"
func main() {
message := map[string]string{
"China": "Beijing",
"Mongolia": "Ulan Bator",
"Finland": "Helsinki",
}
fmt.Println(message)
}
fmt.Println() prints out the key-value pairs of the map:
map[China:Beijing Finland:Helsinki Mongolia:Ulan Bator]
Print array
package main
import "fmt"
func main() {
numbers := [6]int{3, 6, 9, 2, 1}
fmt.Println(numbers)
}
fmt.Println() prints out the contents of the array:
[3 6 9 2 1 0]
Print slices
package main
import "fmt"
func main() {
numbers := []int{3, 6, 9, 2, 1}
fmt.Println(numbers)
}
fmt.Println() prints out the content of the slice:
[3 6 9 2 1]
Print channel
Like the println() function, the fmt.Println() function prints out the address of the channel variable.
package main
import "fmt"
func main() {
demo := make(chan int, 5)
demo <- 3
fmt.Println(demo)
}
operation result:
0xc000130000
Print function
Like the println() function, the fmt.Println() function prints out the address of the variable of the function.
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.Split)
}
operation result:
0x109dbf0