lionsoul2014/ip2region · error

invalid ip value type %s

Error message

invalid ip value type %s

What it means

Go service Search: the ip argument's dynamic type is neither string nor []byte (e.g. net.IP is not accepted here, nor int/nil), so it cannot be converted to raw IP bytes; the message prints the offending value.

Source

Thrown at binding/golang/service/ip2region.go:133

		}
	}

	return NewIp2Region(v4Config, v6Config)
}

func (ip2r *Ip2Region) Search(ip any) (string, error) {
	var err error
	var ipBytes []byte
	switch v := ip.(type) {
	case string:
		ipBytes, err = xdb.ParseIP(v)
		if err != nil {
			return "", fmt.Errorf("parse ip %s: %w", v, err)
		}
	case []byte:
		ipBytes = v
	default:
		return "", fmt.Errorf("invalid ip value type %s", v)
	}

	if l := len(ipBytes); l == 4 {
		return ip2r.v4Search(ipBytes)
	} else if l == 16 {
		return ip2r.v6Search(ipBytes)
	} else {
		return "", fmt.Errorf("invalid byte ip address with len=%d", l)
	}
}

func (ip2r *Ip2Region) v4Search(ipBytes []byte) (string, error) {
	if ip2r.v4InMemSearcher != nil {
		return ip2r.v4InMemSearcher.Search(ipBytes)
	}

	// v4 search is disabled
	if ip2r.v4Pool == nil {

View on GitHub (pinned to c1a1fc7d59)

Solutions

  1. Pass the IP as a string or a 4/16-byte []byte
  2. Convert net.IP with To4()/To16() to get []byte before calling Search
  3. Add a type switch or generics constraint at your own API boundary
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at binding/golang/service/ip2region.go:133 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of lionsoul2014/ip2region@c1a1fc7d59 (2026-09-02). Data as JSON: /api/errors/40bfb1807e5c4cb5. Report an issue: GitHub.