From ae769e61f9bb99ce2b165e261fa4ff90090c3001 Mon Sep 17 00:00:00 2001 From: Dmitriy Shelikhov Date: Fri, 27 Mar 2020 19:57:22 +0700 Subject: [PATCH] fixing the runtime error: index out of range [0] with length 0 --- iputil/geo/geo.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/iputil/geo/geo.go b/iputil/geo/geo.go index 0e01db4..9708936 100644 --- a/iputil/geo/geo.go +++ b/iputil/geo/geo.go @@ -106,12 +106,14 @@ func (g *geoip) City(ip net.IP) (City, error) { if c, exists := record.City.Names["en"]; exists { city.Name = c } - if c, exists := record.Subdivisions[0].Names["en"]; exists { - city.RegionName = c - } - if record.Subdivisions[0].IsoCode != "" { - city.RegionCode = record.Subdivisions[0].IsoCode - } + if len(record.Subdivisions)>0 { + if c, exists := record.Subdivisions[0].Names["en"]; exists { + city.RegionName = c + } + if record.Subdivisions[0].IsoCode != "" { + city.RegionCode = record.Subdivisions[0].IsoCode + } + } if !math.IsNaN(record.Location.Latitude) { city.Latitude = record.Location.Latitude }