Added dotenv parsing, cleared up style errors.
This commit is contained in:
28
main.go
28
main.go
@@ -6,23 +6,37 @@ import (
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
|
||||
"darjeeling.systemec.nl/rhouben/fibonacci/functions"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var port_number int
|
||||
var port_number int = 8098 // hard default
|
||||
var error error
|
||||
var bind_socket string
|
||||
fmt.Println("Hello, world. I'm teaching myself Go.")
|
||||
|
||||
if len(os.Args) <= 1 {
|
||||
port_number = 8088
|
||||
} else {
|
||||
port_number, error = strconv.Atoi(os.Args[1])
|
||||
error = godotenv.Load() // Load .env file
|
||||
if error != nil {
|
||||
fmt.Printf("Error trying to load .env file: %s\n", error)
|
||||
}
|
||||
port_arg := os.Getenv("FIB_PORT")
|
||||
if port_arg != "" {
|
||||
fmt.Printf("Loaded port value '%s' from environment.\n",
|
||||
port_arg)
|
||||
}
|
||||
if len(os.Args) > 1 { // commandline overrules env
|
||||
port_arg = os.Args[1]
|
||||
fmt.Printf("Loading port value '%s' from command line.\n",
|
||||
port_arg)
|
||||
}
|
||||
|
||||
if port_arg != "" {
|
||||
port_number, error = strconv.Atoi(port_arg)
|
||||
if error != nil {
|
||||
fmt.Printf("I had an error trying to parse %s into a number: %s\n",
|
||||
os.Args[1],
|
||||
fmt.Printf("I had an error trying to parse '%s' into a number: %s\n",
|
||||
port_arg,
|
||||
error)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user