From 892f7849c357416ea6d7447db84a2e231190ec08 Mon Sep 17 00:00:00 2001 From: Daniel Cormier Date: Wed, 4 Mar 2020 09:29:26 -0500 Subject: [PATCH] Allow guid package to be used on non-Windows GOOS targets `golang.org/x/sys/windows.GUID` is currently only available to builds targeting `GOOS=windows` (see golang/go#36485). This change makes it so `windows` builds continue to use `golang.org/x/sys/windows.GUID`, while non-`windows` builds get a different structure that has all the same fields and methods. --- pkg/guid/guid.go | 9 --------- pkg/guid/guid_nonwindows.go | 15 +++++++++++++++ pkg/guid/guid_windows.go | 10 ++++++++++ 3 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 pkg/guid/guid_nonwindows.go create mode 100644 pkg/guid/guid_windows.go diff --git a/pkg/guid/guid.go b/pkg/guid/guid.go index 58640657..6e5526bd 100644 --- a/pkg/guid/guid.go +++ b/pkg/guid/guid.go @@ -12,8 +12,6 @@ import ( "encoding/binary" "fmt" "strconv" - - "golang.org/x/sys/windows" ) // Variant specifies which GUID variant (or "type") of the GUID. It determines @@ -39,13 +37,6 @@ type Version uint8 var _ = (encoding.TextMarshaler)(GUID{}) var _ = (encoding.TextUnmarshaler)(&GUID{}) -// GUID represents a GUID/UUID. It has the same structure as -// golang.org/x/sys/windows.GUID so that it can be used with functions expecting -// that type. It is defined as its own type so that stringification and -// marshaling can be supported. The representation matches that used by native -// Windows code. -type GUID windows.GUID - // NewV4 returns a new version 4 (pseudorandom) GUID, as defined by RFC 4122. func NewV4() (GUID, error) { var b [16]byte diff --git a/pkg/guid/guid_nonwindows.go b/pkg/guid/guid_nonwindows.go new file mode 100644 index 00000000..f64d828c --- /dev/null +++ b/pkg/guid/guid_nonwindows.go @@ -0,0 +1,15 @@ +// +build !windows + +package guid + +// GUID represents a GUID/UUID. It has the same structure as +// golang.org/x/sys/windows.GUID so that it can be used with functions expecting +// that type. It is defined as its own type as that is only available to builds +// targeted at `windows`. The representation matches that used by native Windows +// code. +type GUID struct { + Data1 uint32 + Data2 uint16 + Data3 uint16 + Data4 [8]byte +} diff --git a/pkg/guid/guid_windows.go b/pkg/guid/guid_windows.go new file mode 100644 index 00000000..83617f4e --- /dev/null +++ b/pkg/guid/guid_windows.go @@ -0,0 +1,10 @@ +package guid + +import "golang.org/x/sys/windows" + +// GUID represents a GUID/UUID. It has the same structure as +// golang.org/x/sys/windows.GUID so that it can be used with functions expecting +// that type. It is defined as its own type so that stringification and +// marshaling can be supported. The representation matches that used by native +// Windows code. +type GUID windows.GUID