Skip to content

net: net.ParseIP always return ipv6 #65131

@lysShub

Description

@lysShub

Go version

go version go1.21.3 windows/amd64

Output of go env in your module/workspace:

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\lys\AppData\Local\go-build
set GOENV=C:\Users\lys\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=D:\gopath\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=D:\gopath
set GOPRIVATE=
set GOPROXY=https://goproxy.io,direct
set GOROOT=D:\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=D:\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.21.3
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config

What did you do?

import (
	"fmt"
	"net"
	"net/netip"
)

func main() {
	ip1 := net.ParseIP("127.0.0.1")
	fmt.Println([]byte(ip1))

	nip1, _ := netip.AddrFromSlice(ip1)
	fmt.Println(nip1.Is4())

	conn, _ := net.DialUDP("udp", nil, &net.UDPAddr{IP: net.ParseIP("8.8.8.8"), Port: 53})
	ip2 := (conn.LocalAddr().(*net.UDPAddr)).IP
	fmt.Println([]byte(ip2))
}

What did you see happen?

[0 0 0 0 0 0 0 0 0 0 255 255 127 0 0 1]
false
[192 168 0 111]

What did you expect to see?

[127 0 0 1]
false
[192 168 0 111]

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions