
2.4 常用的内置函数
2.4.1 字符串常用内置函数
func strconvDemo() {
v := "10"
if s, err := strconv.Atoi(v); err == nil {
fmt.Printf("%T, %v", s, s)
}
}
func strItoa(){
i := 10
s := strconv.Itoa(i)
fmt.Printf("%T, %v\n", s, s)
}
func ten2others(){
s10 := strconv.FormatInt(v2, 10)
fmt.Printf("%T, %v\n", s10, s10)
s16 := strconv.FormatInt(v2, 16)
fmt.Printf("%T, %v\n", s16, s16)
}
2.4.2 常用的时间和日期相关函数
func getTime(){
//1. 获取当前时间
now := time.Now()
fmt.Printf("Now = %v Type=%T", now, now)
//Now = 2021-02-23 16:14:42.0211031 +0800 CST m=+0.000997301 Type=time.Time
}
func detail(){
//2.获取年月日时分秒
now := time.Now()
fmt.Printf("年=%v\n", now.Year())
fmt.Printf("月=%v\n", now.Month())
fmt.Printf("日=%v\n", int(now.Day()))
fmt.Printf("时=%v\n", now.Hour())
fmt.Printf("分=%v\n", now.Minute())
fmt.Printf("秒=%v\n", now.Second())
}
func timeFormat(){
now := time.Now()
fmt.Println(now.Format("2006/01/02 15:04:05"))
fmt.Println(now.Format("2006-01-02"))
fmt.Println(now.Format("15:04:05"))
}
const (
Nanosecond Duration = 1
Microsecond = 1000 * Nanosecond
Millisecond = 1000 * Microsecond
Second = 1000 * Millisecond
Minute = 60 * Second
Hour = 60 * Minute
)
const (
ANSIC = "Mon Jan _2 15:04:05 2006"
UnixDate = "Mon Jan _2 15:04:05 MST 2006"
RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
RFC822 = "02 Jan 06 15:04 MST"
RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Kitchen = "3:04PM"
// Handy time stamps.
Stamp = "Jan _2 15:04:05"
StampMilli = "Jan _2 15:04:05.000"
StampMicro = "Jan _2 15:04:05.000000"
StampNano = "Jan _2 15:04:05.000000000"
)
func sleepDemo(){
for {
fmt.Println(time.Now().Second())
time.Sleep(time.Millisecond * 100)
}
}
func getUnixTime(){
fmt.Println(time.Now().Unix())
fmt.Println(time.Now().UnixNano())
}
2.4.3 内置函数
len()new()make()
func main() {
num1 := 100
fmt.Printf("num1的类型为:%T, num1的值为:%d, num1的地址为:%p\n", num1, num1, &num1)
num2 := new(int) /*new的返回值是一个指针*/
fmt.Printf("num2的类型为:%T, num2的值为:%d, num2的地址为:%p\n", num2, *num2, &num2)
}