Wed, 29 Apr 2015 6:27 pm
Pindah semua variable di config.go ke file .json , sementara ini path dipakem ke c:\DWConf\config.json, berikutnya mungkin bisa dimasukkan ke argument command line saja #project #go
package config

import(
	"encoding/json"
	"os"
	"fmt"
)

var (

    // Path
    // TemplatePath      = "D:\\PROJECTS\\GO-APP\\sms.0.01\\templates\\"
    // PublicPath		  = "D:\\PROJECTS\\GO-APP\\sms.0.01\\public\\"
    UploadPath		  = "D:\\PROJECTS\\GO-APP\\sms.0.01\\public\\files\\"
    PDFPath			  = "D:\\PROJECTS\\GO-APP\\sms.0.01\\public\\pdf\\"
    ImagesLogoPath 	  = "D:\\PROJECTS\\GO-APP\\sms.0.01\\public\\images\\depkeu.png" // PDF Generator | logo depkeu disposisi suratmasuk
    
    // Tahun Nomor Agenda
    TahunAgendaSMS	  = "2015"

)

type Configuration struct {
    Port string
    TemplatePath string
    PublicPath string
}


func Port() string {
	file, _ := os.Open("C:\\DWConf\\config.json")
	decoder := json.NewDecoder(file)
	configuration := Configuration{}
	err := decoder.Decode(&configuration)
	if err != nil {
	  fmt.Println("error:", err)
	}
	// fmt.Println() // output: [UserA, UserB]
	return configuration.Port
}

func TemplatePath() string {
	file, _ := os.Open("C:\\DWConf\\config.json")
	decoder := json.NewDecoder(file)
	configuration := Configuration{}
	err := decoder.Decode(&configuration)
	if err != nil {
	  fmt.Println("error:", err)
	}
	return configuration.TemplatePath
}

func PublicPath() string {
	file, _ := os.Open("C:\\DWConf\\config.json")
	decoder := json.NewDecoder(file)
	configuration := Configuration{}
	err := decoder.Decode(&configuration)
	if err != nil {
	  fmt.Println("error:", err)
	}
	return configuration.PublicPath
}

44
Additional Info: