1

I'm making a script in golang and in this part of the code I get an error in this line:

syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_NODELAY, 1)

Specifically:

int(fd)

var globalTr = &http.Transport{
    MaxIdleConnsPerHost: 4096,
    MaxIdleConns:        4096,
    MaxConnsPerHost:     4096,
    TLSHandshakeTimeout: 0 * time.Second,
    TLSClientConfig:     &tls.Config{InsecureSkipVerify: true},
    DialContext: (&net.Dialer{
        Control: func(network, address string, c syscall.RawConn) error {
            return c.Control(func(fd uintptr) {
                syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_NODELAY, 1)
            })
        },
    }).DialContext,
}

The error is:

cannot use int(fd) (value of type int) as syscall.Handle value in argument to syscall.SetsockoptInt

esiquiel
  • 37
  • 6
  • The `syscall` package is very platform specific, so you should almost always use it only within files having the correct build constraints. The code looks like it is for unix, while the error is from windows. – JimB Aug 16 '21 at 21:41
  • Note that if your only goal is to set `TCP_NODELAY `, the platform-specific work has already been done for you in [`TCPConn.SetNoDelay`](https://pkg.go.dev/net#TCPConn.SetNoDelay) – JimB Aug 16 '21 at 21:49
  • @JimB I just compiled it in a unix environment and it works perfectly, I'll settle for that. Thanks <3 – esiquiel Aug 16 '21 at 21:53

2 Answers2

0

Depending on your platform syscall's might be prototyped differently.

In this case for Windows the prototype is:

func SetsockoptInt(fd Handle, level, opt int, value int) (err error)

While the prototype in Unix is:

func SetsockoptInt(fd, level, opt int, value int) (err error) {

It appears you are trying to use a Window's prototype on a Unix system, which is giving you a type error because Handle is considered a different type from int.

As noted in the comments syscall functions are specific to the system specified.

See this question for more info on cross-compiling If you aren't cross-compiling you'll need to make sure your environment correctly specifies your target env.

Dweeberly
  • 4,668
  • 2
  • 22
  • 41
0

Ensure, in your editor/ development environment, that the environment variable GOOS is set correctly.

For example, in VSCode, search settings for go.toolsEnvVars. Mine was set to windows from a previous config for a different build target, and that produced the mismatched syscall api.