개발/backend(go)
Go Swagger, 패키지 관리, hash 함수
체인의정석
2025. 4. 8. 11:00
728x90
Go-Swagger 사용법
설치
go get -u github.com/go-swagger/go-swagger/cmd/swagger
주석달기 (쳇지피티 시키면 잘달아줌)
// CreateUserInfo
// @Summary Create new user info
// @Description 새 사용자 정보를 DB에 저장합니다.
// @Tags account
// @Accept json
// @Produce json
// @Param body body protocol.CreateUserRequest true "사용자 정보"
// @Success 200 {object} protocol.RespHeader
// @Failure 500 {object} protocol.RespHeader
// @Router /v1/users/info [post]
Go- swagger 실행
swag init
Swagger 확인 가능한 경로
http://localhost:{port}/swagger/index.html
Go에서 패키지 설치하는법
# 새로운 패키지 설치 + go.mod에 등록
go get <패키지이름>
# go.mod 초기화
go mod init <모듈명>
# 불필요한 의존성 제거 및 정리
go mod tidy
* 단 Go 표준 라이브러리는 설치가 필요하지 않음
import (
"crypto/sha256"
"encoding/hex"
)
표준 라이브러리 예시 hash .go
package utils
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
)
// generateSalt generates a random 16-byte salt
func generateSalt() (string, error) {
saltBytes := make([]byte, 16)
if _, err := rand.Read(saltBytes); err != nil {
return "", err
}
return hex.EncodeToString(saltBytes), nil
}
func hashWithSalt(original string, salt string) string {
data := original + salt
hash := sha256.Sum256([]byte(data))
return hex.EncodeToString(hash[:])
}
728x90
반응형