Go 기간

1 개요[ | ]

Go Time Duration
package main

import (
	"fmt"
	"time"
)

func main() {
	t1, _ := time.Parse(time.RFC3339, "2000-01-01T01:00:00Z")
	t2, _ := time.Parse(time.RFC3339, "2000-01-01T03:00:00Z")
	fmt.Println(t2.Sub(t1)) // 2h0m0s
	fmt.Println(t1.Sub(t2)) // -2h0m0s
}
package main

import (
	"fmt"
	"time"
)

func main() {
	twoSeconds := 2 * time.Second
	twoHours := 2 * time.Hour
	fmt.Println(twoSeconds) // 2s
	fmt.Println(twoHours)   // 2h0m0s
}
package main

import (
	"fmt"
	"time"
)

func main() {
	x := 2
	xHours := time.Duration(x) * time.Hour
	fmt.Println(xHours) // 2h0m0s
}

2 비교[ | ]

package main

import (
	"fmt"
	"time"
)

func main() {
	twoSeconds := 2 * time.Second
	twoHours := 2 * time.Hour
	fmt.Println(twoSeconds < twoHours) // true
	fmt.Println(twoSeconds > twoHours) // false
}
package main

import (
	"fmt"
	"time"
)

func main() {
	zeroSeconds := 0 * time.Second
	zeroHours := 0 * time.Hour
	fmt.Println(zeroSeconds == zeroHours) // true
}

3 같이 보기[ | ]

4 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}