go 1.5.1 crosscompile and runtime.GOOS/runtime.GOARCH

123 views
Skip to first unread message

Vasiliy Tolstov

unread,
Nov 23, 2015, 8:31:03 AM11/23/15
to golang-nuts
Hi! I have sample code:
package guest

import (
"runtime"

"golang.org/x/sys/unix"
)

func newTimeval(t int64) *unix.Timeval {
if runtime.GOARCH != "amd64" {
return &unix.Timeval{Sec: t / 1000000000, Usec:
int32(t) % 1000000000 / 1000}
}
return &unix.Timeval{Sec: t / 1000000000, Usec: t % 1000000000 / 1000}
}

when i'm try to build with GOOS=openbsd and GOARCH=amd64
i have error
cannot use int32(t) % 1000000000 / 1000 (type int32) as type int64 in
field value

when i'm try to build with GOOS=openbsd and GOARCH=386
i have error
cannot use t % 1000000000 / 1000 (type int64) as type int32 in field value


How to assign correct values specific for arch ?
--
Vasiliy Tolstov,
e-mail: v.to...@selfip.ru

Ian Lance Taylor

unread,
Nov 23, 2015, 10:02:49 AM11/23/15
to Vasiliy Tolstov, golang-nuts
You have to write different files for different types, and use build
constraints to pick one which is built on a specific target. For a
case like this the different files could just define a single type,
and your newTimecal function could use use that type in a type
conversion when setting the Sec and Usec fields.

In general this kind of thing is required when using the syscall or
golang.org/x/sys packages.

Ian

Vasiliy Tolstov

unread,
Nov 24, 2015, 10:36:12 AM11/24/15
to Ian Lance Taylor, golang-nuts
2015-11-23 18:01 GMT+03:00 Ian Lance Taylor <ia...@golang.org>:
> You have to write different files for different types, and use build
> constraints to pick one which is built on a specific target. For a
> case like this the different files could just define a single type,
> and your newTimecal function could use use that type in a type
> conversion when setting the Sec and Usec fields.
>
> In general this kind of thing is required when using the syscall or
> golang.org/x/sys packages.


Thanks, this works.
Reply all
Reply to author
Forward
0 new messages