Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separting the development and production environment in GOLANG

Tags:

go

go-iris

I am new to GO programming. I came from nodejs. It was easy to separate the dev and prod mode in nodejs. By simply using this code.

if(process.env.NODE_ENV==="production"){
   server.listen(prod.port);
} 
else{
   server.listen(dev.port);
}

I basically want this convention to use in GO too. So how could I separate my dev and production code.

The reason why want this feature is

to separate the port my server is listening in dev and prod environment

If there is any technique to separate the port, It would work either.

P.S: I am using VScode as my code editor. And go-iris as a go server framework

like image 249
Raju Adhikari Avatar asked Aug 10 '18 06:08

Raju Adhikari


1 Answers

you can use os.Getenv function.

package main

import (
    "fmt"
    "os"
)

func getEnv() string{
    return os.Getenv("APP_ENV")
}

https://golang.org/src/os/env.go?s=2471:2501#L73

like image 152
whitespace Avatar answered Sep 28 '22 03:09

whitespace