Common functions golang --- time and packet type based on conversion

. 1 [] byte converted to string:

package main

import (
	"fmt"
)

func main() {
	data := [4]byte{0x31, 0x32, 0x33, 0x34}
	str := string(data[:])
	fmt.Println(str)
}

  

输出:
1234

2.string into int64 type

package main

import (
	"fmt"
	"strconv"
)

func main() {

	total, _ := strconv.ParseInt("50", 10, 64)
	fmt.Println(total)
}

  

输出:
50

3.golang format to the current date and time:

main Package 

Import ( 
	"FMT" 
	"Time" 
) 

FUNC main () { 

	nowtime: Time.now = () 
	// this is a fixed 2006-01-02 15:04:05 usage Ymd H php language corresponding to: i. : S 
	fmt.Println (nowTime.Format ( "2006-01-02 15:04:05")) 
}

 Output:

 2019-08-18 16:54:57

4. Return current local time:

package main

import (
	"fmt"
	"time"
)

func main() {

	fmt.Println(time.Now())

}

  Output:

2019-08-18 16:57:23.3661001 +0800 CST m=+0.013000801

5. Return the current local time stamp:

package main

import (
	"fmt"
	"time"
)

func main() {

	fmt.Println(time.Now().Unix())

}

  Output:

1566118750

 

 

6. Given time and date string to golang Standard Time:

main Package 

Import ( 
	"FMT" 
	"Time" 
) 

FUNC main () { 

	// given date string 
	nowtime: Time.now = () 
	// this is a fixed 2006-01-02 15:04:05 usage, equivalent to H php language Ymd: I: S 
	X: = nowTime.Format ( "2006-01-02 15:04:05") 
	P, _: = time.Parse ( "2006-01-02 15:04:05" , X) 
	fmt.Println (P) 

	// given timestamp 
	timestamp:. = Time.now () the Unix () 
	fmt.Println (time.Unix (timestamp, 0)) 
}

  

Output:

2019-08-18 17:00:16 +0000 UTC

2019-08-18 17:00:16 +0800 CST

 

7. Calculate running time:

package main

import (
	"fmt"
	"time"
)

func main() {
	t1 := time.Now()

	time.Sleep(time.Second * 2)

	elapsed := time.Since(t1)
	fmt.Println("程序运行时间为: ", elapsed)
}

  Output:

程序运行时间为: 2.0001144s
 

Guess you like

Origin www.cnblogs.com/saryli/p/11373019.html