forked from pariz/gountries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
continent.go
45 lines (36 loc) · 953 Bytes
/
continent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package gountries
import (
"strings"
)
type Continents interface {
FindContinent(continentName string) (Continent, error)
}
type continents struct {
continents []Continent
}
type Continent struct {
Name string
Code string
}
func NewContinents() Continents {
return &continents{
continents: []Continent{
{Code: "AF", Name: "Africa"},
{Code: "AN", Name: "Antarctica"},
{Code: "AS", Name: "Asia"},
{Code: "EU", Name: "Europe"},
{Code: "NA", Name: "North America"},
{Code: "OC", Name: "Oceania"},
{Code: "SA", Name: "South America"},
},
}
}
func (continents *continents) FindContinent(continentName string) (Continent, error) {
cn := strings.ToUpper(continentName)
for _, continent := range continents.continents {
if continent.Code == cn || strings.ToUpper(continent.Name) == cn {
return continent, nil
}
}
return Continent{}, makeError("Could not find continent with given identifier", continentName)
}