Как правильно использовать всю прелесть Golang?
Я сейчас остановился на вопросе, который заключается в том, что в одном проекте, который относится к какой-то направленности у меня подключены разные директории, содержащие инструкции разных кусков.
Так вот, как правильно поступать во время реализации проекта, оставлять все в одном каталоге или же запушить на гит в отдельные репозитории, а потом подключать?
К примеру у меня есть небольшая либа, которая собирает инфу с сервера API одной игрушки:
package champions
import (
"io/ioutil"
"encoding/json"
"fmt"
"net/http"
)
type Skin struct {
// Champion skin struct for storing information about a specific skin
Image string // set after deserializing json structure
Name string `json:"name"`
ID int `json:"num"`
}
type Champion struct {
// Champion structure, contains all information
// about a specific character from the game
Name string `json:"name"`
Title string `json:"title"`
Lore string `json:"lore"`
Skins []Skin `json:"skins"`
// in the json structure a lot more information about a
// specific champion, but we choose the one that we will use later
// Example: http://ddragon.leagueoflegends.com/cdn/10.19.1/data/en_US/champion/Yasuo.json
}
// sets paths to skin images stored on the Riot server
func (c Champion) GetSkinImagesPath() {
for pos := range c.Skins {
c.Skins[pos].Image = fmt.Sprintf(ChampionsImages, c.Name, c.Skins[pos].ID)
}
}
// inclusive structure: data":{"Yasuo":{"id":"Yasuo", ...}
type Content struct { Data map[string]Champion `json:"data"` }
// collects information into the received structure by the received champion name
func Collect(championName string, content *Content) error {
r, err := http.Get(fmt.Sprintf(ChampionsInfo, championName))
if err != nil { return err }
// reading (Unmarshal takes a byte set)
readedBody, err := ioutil.ReadAll(r.Body)
// loading information into a struct
if err := json.Unmarshal(readedBody, content); err != nil {
return err
}
return nil
}
В мейновом файле я ее использую таким образом, каким предполагалось использование этой либы во время ее написания:
func writeChampionName(writer http.ResponseWriter, request *http.Request) {
var championData champions.Content
t, err := template.ParseFiles("test.html")
if err != nil { log.Fatal(err); return }
if request.Method == "POST" {
name := request.FormValue("name")
if err := champions.Collect(name, &championData); err != nil {
log.Fatal(err)
return
}
champion := championData.Data[name]
champion.GetSkinImagesPath()
}
t.Execute(writer, nil)
}
Понятное дело, что многие поступают так, как видят и как им пожелается, но все таки где ответ, содержащий в себе самый правильный поступок?
Я не думаю, что гуглы, создавая этот язык, собирались держать свои либы с кусками бизнес логики, предполагает ли этот язык такой подход к проектированию?