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]

Comment From: mateusz834

This is intentional, [0 0 0 0 0 0 0 0 0 0 255 255 127 0 0 1] is a v4InV6 address.

https://github.com/golang/go/blob/8e658eee9c7a67a8a79a8308695920ac9917566c/src/net/ip.go#L27-L36

EDIT: but maybe it should be documented better?

Comment From: thediveo

Oh yes, this should be documented better! 🩹 🤕

I've been bitten in the past on this, more so, when combining with 3rd party modules. I'm basically now having always code in place that converts back to the IPv4 4-octets representation upon detecting this situation, using this as my canonical IPv4 representation anywhere, especially before comparing type IPs, coming from different sources, such as stdlib and, for instance, RTNETLINK...

Comment From: gopherbot

Change https://go.dev/cl/598076 mentions this issue: net: document ParseIP always returns IPv6 addresses