카테고리 없음

Golang vol.2 (Live Reaload, DB)

곽빵 2022. 11. 27. 15:12

개요

고랭 학습하면서 느낀점이나 기록하면 좋을법한 내용들을 정리

Live Reloading을 위해서는 이하를 참조

https://github.com/cosmtrek/air

 

GitHub - cosmtrek/air: ☁️ Live reload for Go apps

☁️ Live reload for Go apps. Contribute to cosmtrek/air development by creating an account on GitHub.

github.com

GoLang의 데이터베이스에 연결하기 위해 설치한 패키지

npm 같이 이렇게 명령어를 칠 수 있다. (https://go.dev/dl/) <- 물론 여기서 go를 설치한 뒤에

go get -u gorm.io/gorm

go get -u gorm.io/driver/mysql

model에 json으로 반환할 때 키값을 지정

package models

import "golang.org/x/crypto/bcrypt"

type User struct {
	Id           uint   `json:"id"`
	FirstName    string `json:"first_name"`
	LastName     string `json:"last_name"`
	Email        string `json:"email" gorm:"unique"`
	Password     []byte `json:"-"`
	IsAmbassador bool   `json:"-"`
}

func (user *User) SetPassword(password string) {
	hashedPassword, _ := bcrypt.GenerateFromPassword([]byte(password), 12)
	user.Password = hashedPassword
}

func (user *User) ComparePassword(password string) error {
	return bcrypt.CompareHashAndPassword(user.Password, []byte(password))
}