Previously we looked at creating and assigning variable names. In this chapter, we will learn how to display each variable or processed value on the screen.
The functions provided by Go for printing to the screen are Print(), Println(), and Printf() (the meaning of the functions will be explained later).
The screen output function is implemented in a package called ‘fmt’. To use the screen output feature, your program must include the ‘fmt’ package.
import “fmt”
1. Print() function
The Print() function displays the value inside () on the screen.. The value inside () is called a parameter and is the value passed to the function. In other words, when the value stored in the i variable is passed to the Print() function, the function already implemented in the fmt package prints the passed value on the screen.
package main
import "fmt"
func main() {
var i, j string = "Hello", "World"
fmt.Print(i)
fmt.Print(j)
fmt.Print("\n")
fmt.Print(i, j)
fmt.Print("\n")
fmt.Print(score)
fmt.Print("\n")
fmt.Print("Hi, Tom")
}
$ go run print.go
HelloWorld
HelloWorld
99
Hi, Tom
- 08-09: Prints the value held by each variable. At this time, you can see that two strings are output attached. After the Print() function finishes printing, the cursor is positioned immediately after. As a result, the two variable values are attached and output.
- 10: "\n" prints a new line. It plays the same role as ‘Enter’ in a document editor.
- 11: Multiple values can be passed as parameters to the Print() function. In this case, each parameter is separated by ','.
- 12: For variables that have numeric values such as int and float as well as strings, the corresponding values are output.
- 15: You can pass strings directly as parameters. At this time, place double quotation marks (") around the string.
2. Println() function
The Println() function is similar to Print(), but automatically inserts spaces between parameters and also includes new lines("\n").
package main
import "fmt"
func main() {
var i, j string = "Hello", "World"
fmt.Println(i, j)
fmt.Println(i)
}
$ go run println.go
Hello World
Hello
3. Printf() function
The Printf() function first formats its argument based on the given formatting verb and then prints them.
1) In general formatting
Format | Description |
%v | Prints the value of variable |
%#v | Prints the value in Go-syntax format |
%T | Prints the type of value |
%% | Prints the % sign |
package main
import "fmt"
func main() {
var i = 99.9
var str = "Hello World"
fmt.Printf("%v\n", i)
fmt.Printf("%v\n", str)
fmt.Printf("%#v\n", i)
fmt.Printf("%#v\n", str)
fmt.Printf("%T\n", i)
fmt.Printf("%T\n", str)
fmt.Printf("%%\n")
18: }
$ go run printf.go
99.9
Hello World
99.9
"Hello World"
float64
string
%
2) Integer Formatting
Assume variable i contains 10
Formatting | Description | Example |
%b | Base 2 | fmt.Printf("%b\n", i) >> 1010 |
%d | Base 10 | fmt.Printf("%d\n", i) >> 10 |
%+d | Base 10 and always show sign | fmt.Printf("%+d\n", i) >> +10 |
%o | Base 8 | fmt.Printf("%o\n", i) >> 12 |
%O | Base 8, with leading 0o | fmt.Printf("%O\n", i) >> 0o12 |
%x | Base 16, lowercase | fmt.Printf("%x\n", i) >> a |
%X | Base 16, uppercase | fmt.Printf("%X\n", i) >> A |
%#x | Base 16, with leading 0x | fmt.Printf("%#x\n", i) >> 0xa |
%4d | Pad with spaces (width 4, right justified) | fmt.Printf("%4d\n", i) >> __10 ( _ means space ) |
%-4d | Pad with spaces (width 4, left justified) | fmt.Printf("%-4d\n", i) >> 10__ |
%04d | Pad with zeroes (width 4) | fmt.Printf("%04d\n", i) >> 0010 |
3) Float formatting
Assume variable pi contains 3.14
Formatting | Description | Example |
%e | Scientific notation with 'e' as exponent | fmt.Printf("%e\n", pi) >> 3.140000e+00 |
%f | Floating point | fmt.Printf("%f\n", pi) >> 3.140000 |
%.3f | Default width, precision 3 | fmt.Printf("%.3f\n", pi) >> 3.14 |
%6.3f | Width 6, precision 3 | fmt.Printf(“%6.3f\n”, pi) >> __3.14 ( _ means space ) |
4) String formatting
Assume variable str contains “Hello”.
Formatting | Description | Example |
%s | Plain string | fmt.Printf("%s\n", str) >> Hello |
%q | Double-quoted string | fmt.Printf("%q\n", str) >> "Hello" |
%8s | Plain string (width 8, right justified) | fmt.Printf("%.8sf\n", str) >> ___Hello ( _ means space ) |
%-8s | plain string (width 8, left justified) | fmt.Printf(“%-8s\n”, str) >> Hello___ ( _ means space ) |
%x | Hex dump of byte values | fmt.Printf(“%x\n”, str) >> 48656c6c6f |
% x | Hex dump with spaces | fmt.Printf(“% x\n”, str) >> 48 65 6c 6c 6f |
'Programming > Easy Go' 카테고리의 다른 글
Using Conditional Statements: if / switch (0) | 2025.01.09 |
---|---|
Operators(Arithmetic, Comparison, Logical) (1) | 2024.02.07 |
Variables and Constants (0) | 2023.09.08 |
Setup Go development environments (0) | 2023.09.06 |
About Go Language (0) | 2023.09.06 |