https://cafemocamoca.tistory.com/288
[Go] A Tour of Go Exercise : Stringers 풀이
tour.golang.org/methods/18 A Tour of Go tour.golang.org stringer에 대해서 먼저 알아보자면 fmt 패키지에 정의된 인터페이스로 흔하게 사용한다. Stringer는 특정 타입을 string으로 묘사할 수 있다. fmt 패키지를 보
cafemocamoca.tistory.com
https://last9.io/blog/golang-stringer-tool/
Golang's Stringer tool | Last9
Learn about how to use, extend and auto-generate Stringer tool of Golang
last9.io
위 두글을 참조하고, goland 에서 사용해봤다.
잘 동작하기는 하는데, 직접 install 을 해야하는 라이브러리가 있기 때문에 ...
도커에 올릴때, install 을 해줘야하지 않을까 우려된다.
로컬에서만 썼었고, 개발도커에는 올려보지 않았음. 사용하다가 굳이 필요가 없어져서 ...
암튼 이런 유의사항이 있을 수 있다.
이걸 왜 사용했냐면

genrate 하면 해당 구조체의 메소드가 자동으로 생성
const (
MOVIE 변수 = iota
VOD
GROWTH
CUSTOM
)
이런식으로 열거형을 쓸때,
package enum
type OrderByEnum int
const (
None OrderByEnum = 0
New = 1
Old = -1
)
func (r OrderByEnum) String() string {
switch r {
case -1:
return "old"
case 1:
return "new"
default:
return "none"
}
}
func OrderByToEnum(orderBy string) OrderByEnum {
switch orderBy {
case "new":
return New
case "old":
return Old
default:
return None
}
}
이렇게 사용하는 분도 있었는데,
stringer를 쓰면 자동으로 만들어주긴한다.
'Web > GoLang' 카테고리의 다른 글
| [gorm] mysql json 구조 쿼리 및 JSON_OVERLAPS (0) | 2023.09.05 |
|---|---|
| [golang] time 타입 parsing 오류 , gorm bodyparse 해결방안 (0) | 2023.09.01 |
| [golang] time 타입 parsing 오류 (parsing time "~~~" as "~~~": cannot parse "~~~" as "~~~") (0) | 2023.08.22 |
| [gorm] many2many Association 관계 (0) | 2023.08.16 |
| [golang] errors.Is 재정의시 사용법 (0) | 2023.07.26 |
댓글