Go 문자열 포맷팅

Jmnote (토론 | 기여)님의 2021년 4월 29일 (목) 23:18 판
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요

Go 문자열 포맷팅
package main
import "fmt"
func main() {
    fmt.Printf("%t\n", true)  // true

    fmt.Printf("%d\n", 123)   // 123
    fmt.Printf("%b\n", 14)    // 1110
    fmt.Printf("%c\n", 33)    // !
    fmt.Printf("%x\n", 456)   // 1c8
    fmt.Printf("%f\n", 78.9)  // 78.900000

    fmt.Printf("%e\n", 123400000.0)  // 1.234000e+08
    fmt.Printf("%E\n", 123400000.0)  // 1.234000E+08
}
package main
import (
    "fmt"
    "os"
)
func main() {
    fmt.Printf("%s\n", "\"string\"")  // "string"
    fmt.Printf("%q\n", "\"string\"")  // "\"string\""
    fmt.Printf("%x\n", "hex this")    // 6865782074686973

    fmt.Printf("|%6d|%6d|\n", 12, 345)          // |    12|   345|
    fmt.Printf("|%6.2f|%6.2f|\n", 1.2, 3.45)    // |  1.20|  3.45|
    fmt.Printf("|%-6.2f|%-6.2f|\n", 1.2, 3.45)  // |1.20  |3.45  |
    fmt.Printf("|%6s|%6s|\n", "foo", "b")       // |   foo|     b|
    fmt.Printf("|%-6s|%-6s|\n", "foo", "b")     // |foo   |b     |

    s := fmt.Sprintf("a %s", "string")
    fmt.Println(s)  // a string

    fmt.Fprintf(os.Stderr, "an %s\n", "error")  // an error
}
package main
import "fmt"

type point struct {
    x, y int
}

func main() {
    p := point{1, 2}

    fmt.Printf("%v\n",  p)  // {1 2}
    fmt.Printf("%+v\n", p)  // {x:1 y:2}
    fmt.Printf("%#v\n", p)  // main.point{x:1, y:2}
    fmt.Printf("%T\n",  p)  // main.point
    fmt.Printf("%p\n", &p)  // 0xc000016060
}

2 같이 보기

3 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}