22 lines
345 B
Go
22 lines
345 B
Go
|
package game
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
func NewGameFromFile(fileName string) (*Game, error) {
|
||
|
jsonBytes, err := os.ReadFile(fileName)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
var gm Game
|
||
|
if err := json.Unmarshal(jsonBytes, &gm); err != nil {
|
||
|
return nil, fmt.Errorf("%s: %v\n", fileName, err)
|
||
|
} else {
|
||
|
return &gm, nil
|
||
|
}
|
||
|
}
|