Golang学习 - 格式化打印字符串

1. 格式化打印字符串

代码,

package main

import "fmt"

type Product struct {
	Name, Category string
	Price          float64
}

var Kayak = Product{
	Name:     "Kayak",
	Category: "Watersports",
	Price:    275,
}
var Products = []Product{
	{"Kayak", "Watersports", 279},
	{"Lifejacket", "Watersports", 49.95},
	{"Soccer Ball", "Soccer", 19.50},
	{"Corner Flags", "Soccer", 34.95},
	{"Stadium", "Soccer", 79500},
	{"Thinking Cap", "Chess", 16},
	{"Unsteady Chair", "Chess", 75},
	{"Bling-Bling King", "Chess", 1200},
}

func getProductName(index int) (name string, err error) {
	if len(Products) > index {
		// fmt.Sprintf() This function returns a string
		name = fmt.Sprintf("name of product: %v", Products[index].Name)
	} else {
		err = fmt.Errorf("error for index %v", index)
	}
	return
}

func Printfln(template string, values ...interface{}) {
	fmt.Printf(template+"\n", values...)
}

func main() {
	fmt.Println("Product:", Kayak.Name, "Price:", Kayak.Price)
	fmt.Print("Product:", Kayak.Name, "Price:", Kayak.Price, "\n")
	fmt.Printf("Product: %v, Price: $%4.2f", Kayak.Name, Kayak.Price)
	fmt.Println()
	
	fmt.Println()
	name, _ := getProductName(1)
	fmt.Println(name)
	_, err := getProductName(10)
	fmt.Println(err.Error())

	fmt.Println()
	// %v This verb displays the default format for the value.
	Printfln("%%v Value: %v", Kayak)
	// %+v includes field names when writing out struct values.
	Printfln("%%+v Value with fields: %+v", Kayak)
	// %#v This verb displays a value in a format that could be used to re-create the value in a Go code file.
	Printfln("Go syntax: %#v", Kayak)
	Printfln("Type: %T", Kayak)

	fmt.Println()
	number := 250
	// %b This verb displays an integer value as a binary string
	Printfln("Binary: %b", number)
	// %d This verb displays an integer value as a decimal string. This is the default format for integer values, applied when the %v verb is used.
	Printfln("Decimal: %d", number)
	// %o, %O These verbs display an integer value as an octal string. The %O verb adds the 0o prefix.
	Printfln("Octal: %o, %O", number, number)
	// %x, %X These verbs display an integer value as a hexadecimal string. The letters A–F are displayed in lowercase by the %x verb and in uppercase by the %X verb.
	Printfln("Hexadecimal: %x, %X", number, number)

	fmt.Println()
	number2 := 279.00
	// %b This verb displays a floating-point value with an exponent and without a decimal place.
	Printfln("Decimalless with exponent: %b", number2)
	// %e, %E These verbs display a floating-point value with an exponent and a decimal place. The %e uses a lowercase exponent indicator, while %E uses an uppercase indicator.
	Printfln("Decimal with exponent: %e", number2)
	// %f, %F These verbs display a floating-point value with a decimal place but no exponent. The %f and %F verbs produce the same output.
	Printfln("Decimal without exponent: %f", number2)
	// %x, %X These verbs display a floating-point value in hexadecimal notation, with lowercase (%x) or uppercase (%X) letters.
	Printfln("Hexadecimal: %x, %X", number2, number2)

	fmt.Println()
	number3 := 279.00
	// + This modifier (the plus sign) always prints a sign, positive or negative, for numeric values.
	Printfln("Sign: >>%+.2f<<", number3)
	// 0 This modifier uses zeros, rather than spaces, as padding when the width is greater than the number of characters required to display the value.
	Printfln("Zeros for Padding: >>%010.2f<<", number3)
	// - This modifier (the subtracts symbol) adds padding to the right of the number, rather than the left.
	Printfln("Right Padding: >>%-8.2f<<", number3)

	fmt.Println()
	name2 := "Kayak"
	// %s This verb displays a string
	Printfln("String: %s", name2)
	// %c This verb displays a character
	Printfln("Character: %c", []rune(name2)[0])
	// %U This verb displays a character in the Unicode format so that the output begins with U+ followed by a hexadecimal character code
	Printfln("Unicode: %U", []rune(name2)[0])

	fmt.Println()
	// %t This verb formats bool values and displays true or false.
	Printfln("Bool: %t", len(name) > 1)
	Printfln("Bool: %t", len(name) > 100)

	fmt.Println()
	name3 := "Kayak"
	// %p This verb displays a hexadecimal representation of the pointer’s storage location
	Printfln("Pointer: %p", &name3)
}

输出结果,

Product: Kayak Price: 275
Product:KayakPrice:275
Product: Kayak, Price: $275.00

name of product: Lifejacket
error for index 10

%v Value: {Kayak Watersports 275}
%+v Value with fields: {Name:Kayak Category:Watersports Price:275}
Go syntax: main.Product{Name:"Kayak", Category:"Watersports", Price:275}
Type: main.Product

Binary: 11111010
Decimal: 250
Octal: 372, 0o372
Hexadecimal: fa, FA

Decimalless with exponent: 4908219906392064p-44
Decimal with exponent: 2.790000e+02
Decimal without exponent: 279.000000
Hexadecimal: 0x1.17p+08, 0X1.17P+08

Sign: >>+279.00<<
Zeros for Padding: >>0000279.00<<
Right Padding: >>279.00  <<
String: Kayak
Character: K
Unicode: U+004B

Bool: true
Bool: false

Pointer: 0xc0000142e0

完结!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值