Go언어에서의 DDD

Reference https://dev.to/stevensunflash/using-domain-driven-design-ddd-in-golang-3ee5 https://github.com/victorsteven/food-app-server DDD(Domain Driven Design) DDD의 4Layers Domain : domain이 위치하고, 애플리케이션의 비즈니스로직이 정의된 곳 Infrastructure : 애플리케이션과 독립적인 모든 것이 존재하는 곳(외부 라이브러리, 데이터베이스 엔진 등) Application : domain과 interface 사이의 통로. interface layer에서 domain layer로 요청을 보내고 응답을 반환 Interface : 웹 서비스, RIM 인터페이스 웹 애플리케이션, 배치 프로세스 등 다른 시스템과 상호작용 하는 모든 것이 위치 시작 이 프로젝트는 구조는 아래와 같다....

October 28, 2021 · 10 min · icecat471

[Go언어] gorilla websocket request origin 에러

websocket: request origin not allowed by Upgrader.CheckOrigin 위 에러 발생시 upgrader.CheckOrigin = func(r *http.Request) bool { return true } 위 코드를 추가해준다. 필요에 따라 origin check 로직을 추가해주면 된다.

June 2, 2021 · 1 min · icecat471

[Go언어] Closure

클로저(closure) 함수를 일급객체로 취급하는 함수형 프로그래밍 언어(Erlang, Scala, Haskell 등)에서 사용되는 중요한 특성이다.클로저에 대해 MDN은 아래와 같이 정의하고 있다. “A closure is the combination of a function and the lexical environment within which that function was declared.” “클로저는 함수와 그 함수가 선언된 렉시컬 환경의 조합이다.” 아래의 예제를 먼저 확인해 보자 package main func outerFunc() func() { x := 10 innerFunc := func() { fmt.Println(x) } return innerFunc } func main() { f := outerFunc() f() // output: // 10 } scope는 함수를 호출할 때가 아니라 어디에 선언하였는지에 따라 결정된다....

May 25, 2021 · 2 min · icecat471

[Go언어] 슬라이스에서 요소 삭제

순서가 중요한 경우 package main import "fmt" func remove(slice []int, s int) []int { return append(slice[:s], slice[s+1:]...) } func main() { var Slice1 = []int{1, 2, 3, 4, 5} fmt.Printf("slice1: %v\n", Slice1) Slice2 := remove(Slice1, 2) fmt.Printf("slice2: %v\n", Slice2) } 실행 결과 slice1: [1 2 3 4 5] slice2: [1 2 4 5] 순서가 중요하지 않은 경우 package main import "fmt" func remove(s []int, i int) []int { s[i] = s[len(s)-1] return s[:len(s)-1] } func main() { var Slice1 = []int{1, 2, 3, 4, 5} fmt....

May 22, 2021 · 1 min · icecat471

[디자인패턴] Decorator 패턴

Decorator Pattern 출처: https://golangbyexample.com/decorator-pattern-golang/ 개요 개체를 변경하지 않고 기능을 추가할 수 있음. => 이미 테스트가 끝난 코드를 수정하면 안됨 (Open-Closed Principle에 위배됨) 두가지 피자가 존재한다고 가정해보자. vegge mania pizza peppy tofu pizza UML 다이어그램 코드 pizza.go package main type pizza interface { getPrice() int } peppyPaneer.go package main type peppyPaneer struct { } func (p *peppyPaneer) getPrice() int { return 20 } veggeMania.go package main type veggeMania struct { } func (p *veggeMania) getPrice() int { return 15 } 토핑을 추가하기 위해 위쪽에 만들어진 구조체들은 더 이상 수정하면 안됨....

April 21, 2021 · 2 min · icecat471

[디자인패턴] Composite 패턴

Composite Pattern 출처: https://golangbyexample.com/composite-design-pattern-golang/ 개요 ‘composite’라고 불리는 개체그룹이 단일개체와 유사한 방식으로 처리되기를 원할때 사용. 트리구조로 객체들을 엮는다. UML 다이어그램 OS의 파일시스템에는 폴더와 파일 두가지 유형의 개체가 있는데, 폴더와 파일은 동일하게 취급받는 경우가 있다. Mapping Component interface component.go Composite folder.go Leaf file.go client main.go 코드 component.go package main type component interface { search(string) } folder....

April 20, 2021 · 2 min · icecat471

[디자인패턴] Bridge 패턴

Bridge Pattern 출처: https://golangbyexample.com/bridge-design-pattern-in-go/ 개요 구현부에서 추상층을 분리하여 각자 독립적으로 변형할 수 있게 하는 패턴이다.이 패턴은 큰 클래스를 두개의 개별 계층으로 나누는것을 제안한다. Abstraction: interface. Implementation에 대한 참조가 포함됨. Abstraction의 자식을 Refined Abstraction 이라고 부름. Implementation: interface. Implementation의 자식을 Concrete Implementation 이라고 부름. UML 다이어그램 2가지 유형의 컴퓨터 Mac과 Windows가 있다고 가정. 2가지 유형의 프린터 epson과 hp가 있다고 가정. 2*2의 조합의 4개의 구조체를 만드는 대신 2개의 계층을 만든다....

April 19, 2021 · 2 min · icecat471

[디자인패턴] Adapter 패턴

Adapter Pattern 한 클래스의 인터페이스를 클라이언트에서 사용하고자하는 다른 인터페이스로 변환한다.어댑터를 이용하면 인터페이스 호환성 문제 때문에 같이 쓸 수 없는 클래스들을 연결해서 쓸 수 있다.코드 출처: https://refactoring.guru/design-patterns/adapter/go/example 윈도우에는 USB, 맥북에는 thunderbolt 포트가 있다.윈도우 PC에 thunderbolt포트를 연결하기 위해 adapter가 필요하다client.go package main import "fmt" type client struct { } func (c *client) insertLightningConnectorIntoComputer(com computer) { fmt.Println("Client inserts Lightning connector into computer.") com.insertIntoLightningPort() } computer.go package main type computer interface { insertIntoLightningPort() } mac....

April 18, 2021 · 1 min · icecat471

[디자인패턴] Singleton 패턴

Singleton 패턴 하나의 클래스에 대해 하나의 인스턴스만 존재하는 패턴go에서는 sync.Once를 통해 구현 가능하다. package main import ( "fmt" "sync" ) type User struct { uid int name string level int } type userDatabase struct { users map[int]*User } var once sync.Once var instance *userDatabase func (d *userDatabase) GetUser(uid int) (*User, bool) { user, ok := d.users[uid] return user, ok } func GetUserDatabase() *userDatabase { // sync.Once.Do()는 딱 한번만 실행 once....

April 18, 2021 · 2 min · icecat471

[디자인패턴] Prototype 패턴

프로토타입 패턴 프로토타입 패턴(prototype pattern)은 생성할 객체들의 타입이 프로토타입인 인스턴스로부터 결정되도록 하며, 인스턴스는 새 객체를 만들기 위해 자신을 복제(clone)하게 된다.얕은 복사(shallow copy) vs 깊은 복사(deep copy) 얕은 복사 객체를 복사할때 참조값을 가진 멤버는 참조값만 복사 됨. type Status struct { str int dex int wis int } type Monster struct { Name string Status *Status } func main() { slime := Monster{ "slime", &Status{1,1,1}, } goblin := slime goblin.Name = "goblin" goblin....

April 17, 2021 · 2 min · icecat471