목록개발/Golang (2)
EPguy
1. 코드 main.go package main import ( "flag" "log" "net/http" "path/filepath" "sync" "text/template" ) type templateHandler struct { once sync.Once filename string templ *template.Template } func (t *templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { t.once.Do(func() { t.templ = template.Must(template.ParseFiles(filepath.Join("templates", t.filename))) }) t.templ.Execute(w, r) } ..
1. Command-line flags 란? 커맨드 라인 플래그(command-line flags)는 커맨드 라인에서 프로그램에 옵션을 전달하는 방법이다. 먼저 flag를 받기위해선 String 메소드를 사용해야한다. 첫번째 인자로는 Option의 이름, 두번째 인자로는 Option 입력을 안했을때 Default Value, 세번째 인자로는 --help 명령어 입력했을 시 보여줄 옵션에 대한 설명 그 후 flag.Parse() 로 입력된 flag를 파싱해줘야한다. (이 메소드는 반드시 flag가 정의가 된 후, flag 값에 접근하기 전 호출되어야한다.) 이 때 flag.String은 주소를 반환하기 때문에 값 참조 시 포인터로 접근하면 된다. package main import ( "flag" "fmt..