16进制编码
package main
import (
"encoding/hex"
"fmt"
)
func main() {
bs := []byte{0x12, 0x34, 0xef}
str := hex.EncodeToString(bs)
bs2, _ := hex.DecodeString(str)
fmt.Println(bs)
fmt.Println(str)
fmt.Println(bs2)
}
base64编码
package main
import (
"encoding/base64"
"fmt"
)
func main() {
bs := []byte{0xff, 0xef, 0xff}
str := base64.StdEncoding.EncodeToString(bs)
strURL := base64.URLEncoding.EncodeToString(bs)
bs2, _ := base64.StdEncoding.DecodeString(str)
bs3, _ := base64.URLEncoding.DecodeString(strURL)
fmt.Println(str)
fmt.Println(strURL)
fmt.Println(bs2)
fmt.Println(bs3)
}
flag包设置命令行选项
package main
import (
"flag"
"fmt"
)
var n = flag.Bool("n", false, "print index")
var sep = flag.String("s", "|", "separator")
func main() {
flag.Parse()
if *n {
for i, arg := range flag.Args() {
fmt.Print(i, arg, *sep)
}
} else {
for _, arg := range flag.Args() {
fmt.Print(arg, *sep)
}
}
}