Package fmt implements formatted I/O with functions analogous to C's printf and scanf.
Description
General
- %v : the value in a default format (when printing structs, adds the plus symbol to field names <%+v>)
- %#v : a Go-syntax representation of the value
- %T : a Go-syntax representation of the type of value
Integer
- %b : base 2
- %d : base 10
- %x : base 16, with lower-case letters for a-f
- %X : base 16, with upper-case letters for A-F
Floating-Point and Complex Constituents
- %f : decimal point but no exponent
- %w.pf : where w is width and p is precision
String and Slice of Bytes
- %s : the uninterpreted bytes of the string or slice
- %q : a double-quoted string safely escaped with Go syntax
Pointer
- %p : base 16 notation, with leading 0x
Methods
Errorf
Errorf formats according to a format specifier and returns the string as a value that satisfies error.
func Errorf(format string, a ...interface{}) error
Print formats using the default formats for its operands and writes to standard output.
Spaces are added between operands when neither is a string.
It returns the number of bytes written and any write error encountered.
func Print(a ...interface{}) (n int, err error)
Printf
Printf formats according to a format specifier and writes to standard output.
It returns the of bytes written and any write error encountered.
func Printf(format string, a ...interface{}) (n int, err error)
Println
Println formats using the default formats for its operands and writes to standard output.
Spaces are always added between operands and a new line is appended.
It returns the number of bytes written and any write error encountered.
func Println(a ...interface{}) (n int, err error)
Sprintf
Sprintf formats according to a format specifier and returns the resulting string.
func Sprintf(format string, a ...interface{}) string
Scan
Scan scans text read from standard input, storing successive space-separated values into successive arguments.
func Scan(a ...interface{}) (n int, err error)
Scanf
Scanf scans text read from standard input, storing successive space-separated values into successive arguments as determined by the format.
func Scanf(format string, a ...interface{}) (n int, err error)
Scanln
Scanln is similar to Scan, but stops scanning at a newline and after the final item there must be a newline or EOF.
func Scanln(a ...interface{}) (n int, err error)
Usage
package main
import "fmt"
func main() {
fmt.Printf("%7.2f\n", 3.141592)
str := fmt.Sprintf("%0.3f", 2.71828)
fmt.Print("Test: ")
fmt.Println(str)
// Width, Padding, Alignment
fmt.Printf("%05d\n", 18)
fmt.Printf("%-05d\n", 18)
// Scan
stdin := bufio.NewReader(os.Stdin)
var a int
var b int
n, err := fmt.Scanln(&a, &b)
if err != nil {
fmt.Printf("%v\n", err)
stdin.ReadString('\n')
} else {
fmt.Printf("%v, %v, %#v\n", n, a, b)
}
}
// 3.14
// Test: 2.718
// 00018
// 18
'Go > Packages' 카테고리의 다른 글
[Go] Package os (0) | 2020.12.02 |
---|---|
[Go] Package testing (0) | 2020.11.30 |
[Go] Package log (0) | 2020.11.30 |
[Go] Package bufio (0) | 2020.10.31 |
[Go] Package errors (0) | 2020.10.29 |
댓글