Skip to content

net: Windows build with netgo doesn't repsect hosts file, can not resolve localhost #57757

Closed
@wxiaoguang

Description

@wxiaoguang

What version of Go are you using (go version)?

$ go version
go version go1.19.5 windows/amd64
go version go1.20rc2 windows/amd64

What did you do?

Set Windows system DNS to a standard server which doesn't resolve localhost, then use Go net package to resolve a domain defined in local hosts file (C:\Windows\System32\Drivers\etc\hosts).

package main

import (
	"fmt"
	"net"
)

func main() {
	addr, err := net.ResolveTCPAddr("tcp", "localhost:12345")
	fmt.Printf("addr=%v, err=%v\n", addr, err)
}

What did you expect to see?

Go should respect C:\Windows\System32\Drivers\etc\hosts file and resolve localhost correctly with -tags=netgo.

What did you see instead?

Go can not resolve localhost with -tags=netgo

C:\work> go run resolvtest.go
addr=127.0.0.1:12345, err=<nil>

C:\work> go run -tags=netgo resolvtest.go
addr=<nil>, err=lookup localhost on 8.8.8.8:53: no such host

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.OS-Windows

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions