How can I get unix timestamp(time.Now().Unix()) in linker(cmd/link)

147 views
Skip to first unread message

Ally Dale

unread,
Dec 15, 2017, 6:08:29 AM12/15/17
to golang-nuts
Hi all,
I just need a function to report the timestamp when my app compiled.
func main() {

        unix := runtime.BuildTimestamp()

        t := time.Unix(unix, 0)

        println("hello world")

        println("buildtimestamp:", unix)

        println("buildtime     :", t.String())

        // Output:

        // hello world

        // buildtimestamp: 1513329454

        // buildtime     : 2017-12-15 17:17:34 +0800 CST

}


use a variable defined in [runtime.firstmoduledata.buildtimestamp](https://github.com/vipally/go/blob/ally_build_time/src/runtime/symtab.go#L372)
which will be written by

My trouble is, if cmd/link use time.Now().Unix() to get unix timestamp, it will make a new dependence of time for cmd/link.
And this dependence will cause a STALE error when go_bootstrap building.
go tool dist: unexpected stale targets reported by E:\go\pkg\tool\windows_amd64\go_bootstrap list -gcflags="" -ldflags=""
for [cmd/asm cmd/cgo cmd/compile cmd/link runtime/internal/sys]:
        STALE cmd/asm: stale dependency: runtime/internal/atomic
        STALE cmd/cgo: stale dependency: runtime/internal/atomic
        STALE cmd/compile: stale dependency: runtime/internal/atomic
        STALE cmd/link: stale dependency: runtime/internal/atomic
        STALE runtime/internal/sys: build ID mismatch


So, my question is, if I cannot use time.Now().Unix() or runtime.now() to get unix timestamp in cmd/link. How can I get unix timestamp in linker(cmd/link)?

Ian Lance Taylor

unread,
Dec 15, 2017, 4:11:00 PM12/15/17
to Ally Dale, golang-nuts
Use something like -ldflags=-X=`VAR=date`. The -X option is
documented at https://golang.org/cmd/link.

Ian

Ally Dale

unread,
Dec 17, 2017, 10:11:08 PM12/17/17
to golang-nuts
Thanks a lot, but I tried to pass timestamp by "-X=runtime.buildtimestamp=xxxx" to linker, it fails with the same message.
I guess the timestamp makes the dependence packages always STALE, maybe I have to check logic of command "go list".

 

在 2017年12月16日星期六 UTC+8上午5:11:00,Ian Lance Taylor写道:

Ian Lance Taylor

unread,
Dec 20, 2017, 10:33:04 PM12/20/17
to Ally Dale, golang-nuts
On Sun, Dec 17, 2017 at 7:11 PM, Ally Dale <vip...@gmail.com> wrote:
>
> Thanks a lot, but I tried to pass timestamp by
> "-X=runtime.buildtimestamp=xxxx" to linker, it fails with the same message.
> I guess the timestamp makes the dependence packages always STALE, maybe I
> have to check logic of command "go list".

Sorry, I did not mean to suggest that you use something like
-X=runtime.buildtimestamp. I agree that that is problematic. I meant
to suggest that you use the -X option to set a variable in your own
package, not in the runtime package. There's no reason to add a
variable to the runtime package for this.

Ian
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages