首页 > 其他分享 >go打印字符串

go打印字符串

时间:2023-03-04 14:35:04浏览次数:33  
标签:main string point fmt 打印 字符串 Printf go foo

package main

import (
	"fmt"
	"os"
)

type point struct {
	x, y int
}

func main() {
	p := point{1, 2}
	fmt.Printf("001>>%v\n", p)                      // => {1 2}
	fmt.Printf("002>>%+v\n", p)                     // => {x:1 y:2}
	fmt.Printf("003>>%#v\n", p)                     // => main.point{x:1, y:2}
	fmt.Printf("004>>%T\n", p)                      // => main.point
	fmt.Printf("005>>%t,%T\n", true, true)          // => TRUE
	fmt.Printf("006>>%t,%T\n", false, false)        // => TRUE
	fmt.Printf("007>>%d\n", 123)                    // => 123
	fmt.Printf("008>>%b\n", 14)                     // => 1110
	fmt.Printf("009>>%c\n", 97)                     // => !
	fmt.Printf("010>>%x\n", 456)                    // => 1c8
	fmt.Printf("011>>%f,%T\n", 78.9, 78.9)          // => 78.9
	fmt.Printf("012>>%e\n", 123400000.0)            // => 1.23E+08
	fmt.Printf("013>>%E\n", 1234000000)             // => 1.23E+08
	fmt.Printf("014>>%s\n", "\"string\"")           // => "string"
	fmt.Printf("015>>%q\n", "\"string\"")           // => "\"string\""
	fmt.Printf("016>>%x\n", "hex this")             // => 6.86578E+15
	fmt.Printf("017>>%p\n", &p)                     // => 0xc00002c040
	fmt.Printf("018>>|%6d|%6d|\n", 12, 345)         // => |    12|   345|
	fmt.Printf("019>>|%6.2f|%6.2f|\n", 1.2, 3.45)   // => |  1.20|  3.45|
	fmt.Printf("020>>|%-6.2f|%-6.2f|\n", 1.2, 3.45) // => |1.20  |3.45  |
	fmt.Printf("021>>|%6s|%6s|\n", "foo", "b")      // => |   foo|     b|
	fmt.Printf("022>>|%-6s|%-6s|\n", "foo", "b")    // => |foo   |b     |
	s := fmt.Sprintf("a %s\n", "string")
	fmt.Println("023>>", s)
	fmt.Fprintf(os.Stderr, "an你好啊%s\n", "error")
}

输出:

001>>{1 2}
002>>{x:1 y:2}
003>>main.point{x:1, y:2}
004>>main.point
005>>true,bool
006>>false,bool
007>>123
008>>1110
009>>a
010>>1c8
011>>78.900000,float64
012>>1.234000e+08
013>>%!E(int=1234000000)
014>>"string"
015>>"\"string\""
016>>6865782074686973
017>>0xc0000a2070
018>>|    12|   345|
019>>|  1.20|  3.45|
020>>|1.20  |3.45  |
021>>|   foo|     b|
022>>|foo   |b     |
023>> a string

an你好啊error

标签:main,string,point,fmt,打印,字符串,Printf,go,foo
From: https://www.cnblogs.com/heris/p/17178236.html

相关文章