go 条件编译通过,会的同学请指点一二

2017-02-06 23:38:47 +08:00
 liyu4
// +build windows

package main

import (
	"fmt"

	"syscall"
	"unsafe"
)

var kernel32 = syscall.NewLazyDLL("kernel32.dll")

var (
	procGetConsoleMode = kernel32.NewProc("GetConsoleMode")
)

// IsTerminal returns true if stderr's file descriptor is a terminal.
func IsTerminal() bool {
	fd := syscall.Stderr
	var st uint32
	r, _, e := syscall.Syscall(procGetConsoleMode.Addr(), 2, uintptr(fd), uintptr(unsafe.Pointer(&st)), 0)
	return r != 0 && e == 0
}

func main() {
	fmt.Println("hello")
}

output:

→ go run main.go
# command-line-arguments
./main.go:12: undefined: syscall.NewLazyDLL
./main.go:22: too many arguments in call to syscall.Syscall

放在独立的文件里是可以被编译的,但是放在 main 函数里面怎么就不可以。实在费解,可能我理解的是错的,请会的同学指出我的错误!

3210 次点击
所在节点    问与答
2 条回复
defia
2017-02-06 23:48:16 +08:00
run 就直接 run 这个文件了吧 go build 才会忽略这个
liyu4
2017-02-07 07:40:00 +08:00
@defia go build 也是报这个错误

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/338600

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX