본문 바로가기

Golang

(7)
Golang, 그대들은 어떻게 할 것인가 - Error Wrapping, Handling Golang, 그대들은 어떻게 할 것인가 - 1. 들어가며 Golang, 그대들은 어떻게 할 것인가 - 2. MongoDB Go Driver 추상화 Golang, 그대들은 어떻게 할 것인가 - 3. error 래핑 Golang, 그대들은 어떻게 할 것인가 - 4. error 핸들링
[Golang] Golang에서 error 처리를 어떻게 해야할까 문제 상황 Golang으로 백엔드 개발한지 거의 1년이 되어가고 있는데, 처음 접했을 때부터 지금까지 항상 애매했던게 error 처리 방식이었다. Golang에서는 기본적으로 프로그램이 죽는 exception(a.k.a Fatal Error)가 아닌 이상, 절대 panic(타 언어에서는 exception의 개념정도)을 던지지 말고, return value로 error를 리턴하도록 가이드를 하고 있다. 그래서 대부분 아래와 같이 if 분기문이 많다. num1, err := strconv.Atoi("100") // 문자열 "100"을 숫자 100으로 변환 if err != nil { fmt.Printf("error occured, message: %s", err.Error()) } else { fmt.Pri..
[Golang] MongoDB Wrapper - 2 2022.08.15 - [Golang] - [Golang] MongoDB wrapper [Golang] MongoDB wrapper go-mongo go.mongodb.org/mongo-driver 를 기반으로 실제 프로젝트에서 유용하게 사용될 수 있는 wrapper 어떻게 하다 시작했나? 회사에 입사하고 나서, golang과 몽고 DB를 주로 사용하게 되었다. mongo-driver.. juna-dev.tistory.com https://github.com/kjh03160/go-mongo/pull/25/files generic 디코딩 & not modified 삭제 & 리팩토링 by kjh03160 · Pull Request #25 · kjh03160/go-mongo github.com 이전에 고민했던..
[Golang] MongoDB wrapper go-mongo go.mongodb.org/mongo-driver 를 기반으로 실제 프로젝트에서 유용하게 사용될 수 있는 wrapper 어떻게 하다 시작했나? 회사에 입사하고 나서, golang과 몽고 DB를 주로 사용하게 되었다. mongo-driver 라이브러리를 사용하고 있었으며, 해당 라이브러리와 몽고 DB에 익숙해질 겸 라이브러리 함수를 사용하여 내가 나중에 쓸만한 코드를 만들어 둘려고 했다. 하지만 내가 golang으로 개발하면서 처음부터 그리고 지금까지도 고민되는 부분이 있었다. go에서는 함수에서 에러가 발생하면, 다른 언어들처럼 exception(go에서는 panic)을 발생시키는 것이 아닌 해당 에러를 직접 핸들링을 하는것을 권장한다. func fn() error { x, err := ..
[Golang] Memory Leak 예방하기 http Request response Body should be READ & CLOSED Golang http Client.Do() 문서에 보면 아래와 같은 내용이 명시 If the returned error is nil, the Response will contain a non-nil Body which the user is expected to close. If the Body is not both read to EOF and closed, the Client's underlying RoundTripper (typically Transport) may not be able to re-use a persistent TCP connection to the server for a subsequent "k..
[Golang] golang validator Required ¶ 값이 zero value가 아닌 것인지 확인 → 0, “”, slice나 map같은 건 nil 값이 아니여야함 Usage: required Required With ¶ 다른 필드가 존재하면 해당 필드도 required Usage: required_with Examples: // require the field if the Field1 is present: Usage: required_with=Field1 // require the field if the Field1 **or** Field2 is present: Usage: required_with=Field1 Field2 Required With All ¶ required_with의 and버전 Usage: required_with_a..
[Golang] Gin 유용한 프레임워크 단 코드 Model JSON binding and validation Type Must bind : 바인딩 에러가 있을 경우, 400 에러 및 에러난 바인딩 타입 response (text/plain) Should bind : 바인딩 에러가 있을 경우, 에러 리턴 → 에러 핸들링 // Binding from JSON type Login struct { User string `json:"user" binding:"required"` Password string `json:"password" binding:"required"` } router.POST("/loginJSON", func(c *gin.Context) { var json Login if err := c.ShouldBindJSON(&json); err !=..