Skip to content

Commit

Permalink
Merge pull request #2 from RobinToubi/fix_path
Browse files Browse the repository at this point in the history
fix : use embed to retrieve file when calling InstanceCountry from an…
  • Loading branch information
RobinToubi authored Jun 15, 2023
2 parents 1eb9dce + 415a747 commit 530ec7e
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions countries.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"embed"
)

//go:embed countries.json
var f embed.FS

type CountryData struct {
countries map[string]Country
}
Expand All @@ -20,16 +23,16 @@ type Country struct {

func InstanceCountry() *CountryData {
instance := &CountryData{}
instance = instance.ReadCountriesFile()
instance = instance.readCountriesFile()

return instance
}

func (data *CountryData) ReadCountriesFile() *CountryData {
func (data *CountryData) readCountriesFile() *CountryData {
if len(data.countries) > 0 {
return data
}
file, err := ioutil.ReadFile("countries.json")
file, err := f.ReadFile("countries.json")
if err != nil {
return nil
}
Expand Down

0 comments on commit 530ec7e

Please sign in to comment.