{"record":{"id":"ae3651eeb4d36254","repo":"lionsoul2014/ip2region","slug":"invalid-ip-value-type-s-ae3651","errorCode":null,"errorMessage":"invalid ip value type %s","messagePattern":"invalid ip value type (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"binding/golang/xdb/searcher.go","lineNumber":123,"sourceCode":"// GetIOCount return the global io count for the last search\nfunc (s *Searcher) GetIOCount() int {\n\treturn s.ioCount\n}\n\n// Search the region for the specified string or bytes ip address\nfunc (s *Searcher) Search(ip any) (string, error) {\n\tvar err error\n\tvar ipBytes []byte\n\tswitch v := ip.(type) {\n\tcase string:\n\t\tipBytes, err = ParseIP(v)\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"parse ip %s: %w\", v, err)\n\t\t}\n\tcase []byte:\n\t\tipBytes = v\n\tdefault:\n\t\treturn \"\", fmt.Errorf(\"invalid ip value type %s\", v)\n\t}\n\n\t// ip version check\n\tif len(ipBytes) != s.version.Bytes {\n\t\treturn \"\", fmt.Errorf(\"invalid ip address(%s expected)\", s.version.Name)\n\t}\n\n\t// reset the global ioCount\n\ts.ioCount = 0\n\n\t// locate the segment index block based on the vector index\n\tvar il0, il1 = int(ipBytes[0]), int(ipBytes[1])\n\tvar idx = il0*VectorIndexCols*VectorIndexSize + il1*VectorIndexSize\n\tvar sPtr, ePtr = uint32(0), uint32(0)\n\tif s.vectorIndex != nil {\n\t\tsPtr = binary.LittleEndian.Uint32(s.vectorIndex[idx:])\n\t\tePtr = binary.LittleEndian.Uint32(s.vectorIndex[idx+4:])\n\t} else if s.contentBuff != nil {","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/lionsoul2014/ip2region/blob/c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1/binding/golang/xdb/searcher.go#L105-L141","documentation":"Search only accepts string or []byte ip values. Any other Go type (int, net.IP, netip.Addr, nil, etc.) hits the default branch and returns this error. net.IP is []byte under the hood but a distinct named type, so it also falls through unless converted.","triggerScenarios":"Passing a net.IP value directly to Search; passing an int or uint32; passing nil interface; passing netip.Addr.","commonSituations":"Developers assume net.IP works because it is byte-backed; parsing with netip and forwarding the Addr instead of its As4()/As16() slice.","solutions":["Convert net.IP to []byte: []byte(netIP) or netIP.To4()/To16().","Use addr.AsSlice() for netip.Addr, or a string via addr.String().","Format integers as dotted string only if you really mean an IPv4 int; otherwise convert to 4-byte slice.","Switch to searchByBytes-style flow with an explicitly validated []byte."],"exampleFix":"// before\nregion, err := searcher.Search(ctx, netIP) // net.IP type → error\n// after\nregion, err := searcher.Search(ctx, []byte(netIP.To16()))","handlingStrategy":"type-guard","validationCode":"switch v := ipArg.(type) {\ncase string:\n    if net.ParseIP(v) == nil { return errors.New(\"bad ip string\") }\ncase []byte:\n    if len(v) != 4 && len(v) != 16 { return errors.New(\"bad ip bytes\") }\ndefault:\n    return fmt.Errorf(\"unsupported ip type %T\", ipArg)\n}","typeGuard":"func isSearchableIP(v interface{}) bool {\n    switch t := v.(type) {\n    case string:\n        return net.ParseIP(t) != nil\n    case []byte:\n        return len(t) == 4 || len(t) == 16\n    default:\n        return false\n    }\n}","tryCatchPattern":"region, err := searcher.Search(ctx, ipArg)\nif err != nil && strings.Contains(err.Error(), \"invalid ip value type\") {\n    return fmt.Errorf(\"convert ip to string or []byte first (type %T)\", ipArg)\n}","preventionTips":["Always pass string or []byte — convert net.IP via To4()/To16() or netip.Addr via AsSlice().","Write a small wrapper around Search that accepts net.IP and normalizes it.","Document the accepted types in your lookup helper's signature.","Add a unit test covering each caller's ip argument type."],"tags":["golang","type-error","invalid-argument"],"backgroundTag":"invalid-argument-value","analyzedSha":"c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1","analyzedAt":"2026-09-02T16:58:39.988Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}