0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

golangのTimeについて

Posted at

これ「なんでそんなこと思ったの?」って言われそう。

も大したことはないですが、
「現在時刻を変数に入れたら、数秒たった後って時間って進むの?」
結論から言えば、now := time.Now()をした場合、
その時刻が変数nowに格納されて時間経過で変化がないです。

以下、検証コード。

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()
	// 変数
	fmt.Println("before    :", now.Format("2006-01-02T15:04:05Z07:00"))
	// Timeオブジェクト
	fmt.Println("beforeNow :", time.Now().Format("2006-01-02T15:04:05Z07:00"))
	fmt.Println("2秒待ちます")
	time.Sleep(time.Second * 2)
	// 変数
	fmt.Println("after    :", now.Format("2006-01-02T15:04:05Z07:00"))
	// Timeオブジェクト
	fmt.Println("afterNow :", time.Now().Format("2006-01-02T15:04:05Z07:00"))

}
$ go run main.go
before    : 2021-11-02T12:14:00+09:00
beforeNow : 2021-11-02T12:14:00+09:00
2秒待ちます
after    : 2021-11-02T12:14:00+09:00
afterNow : 2021-11-02T12:14:02+09:00

オブジェクトを表示してるafterNowだけが進んでます。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?