- Go 전용 HTTP 프레임워크, Java Spring의 Go 버전이라 이해하면 될 듯
사용 관련
BindXXX
- Gin에선 요청값 판별을 Context를 통해 수행하는데, 타입별로 JSON, URI 등이 있고, 두 개 분류로 나뉘어진다
- BindXXX : 에러 로깅을 한다, 내부적으로 에러가 발생하면 BadRequest 관련 에러 세팅 및 로깅을 진행
- ShouldBindXXX : 에러 로깅은 하지 않고, 에러 응답만 세팅한다
세팅
로컬 세팅
go get -u github.com/gin-gonic/gin
# gin이 vendor로 패키지 관리를 하고 있어 같이 받아준다
go get github.com/kardianos/govendor
# go 패키지에 별도 프로젝트를 만들어서 페치, 여기선 github.com/ground-x/gonic-test 로 만듬
cd go/src/github.com/ground-x/gonic-test
govendor init
govendor fetch github.com/gin-gonic/[email protected]
# 만약에 govendor cmd가 먹히지 않으면 vi ~/.bash_profile 로 go의 bin path를 추가한다
# export PATH=$PATH:/Users/myoungheejae/go/bin
vi main.go
- main.go는 아래로만 작성하고, 바로 실행한다
- main.go 예시
- 실행하면 8080 포트로 서버가 로드되고, 예시 URL로 호출했을 때 정상 응답을 확인할 수 있다
- 다른 리소스, MySQL이랑 연동할땐 아래처럼 라이브러리를 다운받아 같이 연동할 수 있다
go get -u github.com/go-sql-driver/mysql
create table test_schema.person(
id int AUTO_INCREMENT,
first_name varchar(10) not null,
last_name varchar(10) not null,
primary key(id)
);
- DB 연동 예시 코드
- 추가로 호출하면 아래와 같이 기능 동작을 하는것을 볼 수 있다