标签:转义 golang walk lock fun 16进制 exit 地址 scanf
package main
import (
"fmt"
)
//1
var brand = "ALIENWARE"
type computer struct {
name string
price float32
config []string
brand *string
}
func main() {
myComputer := computer{"tktk", 18000.00, []string{"i7 9700k", "RTX 2080Ti", "DDR4 32G"}, &brand}
fmt.Printf("my computer is: \n%#v\n", myComputer)
fmt.Printf("my computer is: \n%v\n", myComputer)
}
结果:
go version go1.12 linux/amd64
my computer is:
main.computer{name:"tktk", price:18000, config:[]string{"i7 9700k", "RTX 2080Ti", "DDR4 32G"}, brand:(*string)(0x5531b0)}
my computer is:
{tktk 18000 [i7 9700k RTX 2080Ti DDR4 32G] 0x5531b0}
}
// 按照默认格式打印一系列变量
func Print(a ...interface{}) (n int, err error)
// 相比Print,在变量之间插入了空格,并最后添加上换行符
func Println(a ...interface{}) (n int, err error)
// 根据自定义格式化字符串,能打印不同格式的输出
func Printf(format string, a ...interface{}) (n int, err error)
func Fprint(w io.Writer, a ...interface{}) (n int, err error)
func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)
func Fprintln(w io.Writer, a ...interface{}) (n int, err error)
Fprint是更通用的形式,print仅仅是Fprint(os.Stdout, "Hello World!")的包装
func Sprint(a ...interface{}) string
func Sprintf(format string, a ...interface{}) string
func Sprintln(a ...interface{}) string
当大量采用Sprint产生字符串,会产生瓶颈问题。
func Errorf(format string, a ...interface{}) error {
return errors.New(Sprintf(format, a...))
}
fmt包也有从stdin,io.Reader, string读取并格式化的api。
func Scan(a ...interface{}) (n int, err error)
func Scanf(format string, a ...interface{}) (n int, err error)
func Scanln(a ...interface{}) (n int, err error)
func Fscan(r io.Reader, a ...interface{}) (n int, err error)
func Fscanf(r io.Reader, format string, a ...interface{}) (n int, err error)
func Fscanln(r io.Reader, a ...interface{}) (n int, err error)
func Sscan(str string, a ...interface{}) (n int, err error)
func Sscanf(str string, format string, a ...interface{}) (n int, err error)
func Sscanln(str string, a ...interface{}) (n int, err error)
var name string
var age int
if _, err := fmt.Scan(&name, &age); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Your name is: %s\n", name)
fmt.Printf("Your age is: %d\n", age)
执行交互
$ go run main.go
Jane 25
Your name is: Jane
Your age is: 25
总之,如果从命令行读取并解析到指定的interface,如例子中的name,age,我们有更好用的flag包可以使用
标签:转义 golang walk lock fun 16进制 exit 地址 scanf
原文地址:https://www.cnblogs.com/linyihai/p/10727308.html