...
1[!GOOS:windows] skip
2
3go run .
4stdout 'ws2_32.dll: not found'
5
6go run -tags net .
7stdout 'ws2_32.dll: found'
8
9-- go.mod --
10module m
11
12go 1.21
13
14-- utils.go --
15package main
16
17import (
18 "fmt"
19 "syscall"
20 "unsafe"
21)
22
23func hasModuleHandle() {
24 const ws2_32 = "ws2_32.dll"
25 getModuleHandle := syscall.MustLoadDLL("kernel32.dll").MustFindProc("GetModuleHandleW")
26 mod, _, _ := getModuleHandle.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(ws2_32))))
27 if mod != 0 {
28 fmt.Println(ws2_32+":", "found")
29 } else {
30 fmt.Println(ws2_32+":", "not found")
31 }
32}
33-- net.go --
34//go:build net
35package main
36
37import _ "net"
38
39func main() {
40 hasModuleHandle()
41}
42-- nonet.go --
43//go:build !net
44package main
45
46func main() {
47 hasModuleHandle()
48}
View as plain text