티스토리 뷰


개요

 

Protocol Buffers grpc 공부해보고자 한다.

justforfunc 라는 유튜브를 통해서 대략의 개요와 함께 실전 코드로 감을 잡을 있겠다.

 

YouTube: justforfunc #30: The Basics of Protocol Buffers: https://youtu.be/_jQ3i_fyqGA

GitHub: https://github.com/campoy/justforfunc/tree/master/30-protobuf

 

justforfunc #30: The Basics of Protocol Buffers: https://youtu.be/_jQ3i_fyqGA

justforfunc #31: gRPC Basics: https://youtu.be/uolTUtioIrc

justforfunc #32: CLI tools with Cobra: https://youtu.be/WvWPGVKLvR4

justforfunc #33: the Rise And Fall of CORBA: https://youtu.be/zgSZNCltUD0

 

Protocol Buffers - preparation

 

1) 컴파일러 protoc

- 그냥 컴파일 된거 가져와서 고랭의 bin 폴더에 넣어버리면 된다.

- 링크: https://github.com/protocolbuffers/protobuf/releases/ 에서

- protoc-3.8.0-win64.zip 다운받아, 고랭의 bin 폴더에 protoc.exe 옮겨둔게 전부이다.

 

2) protoc-gen-go

- go get -d -u github.com/golang/protobuf/protoc-gen-go

- go install github.com/golang/protobuf/protoc-gen-go

 

* trouble shooting

- 에러메시지: unrecognized import path "protoc-gen-go" (import path does not begin with hostname)

- 해법: 환경변수 GOROOT 잘못 설정되어 있었다. https://stackoverflow.com/a/20459819/6513756

 

전체 흝어 보기

 

1) 주고 받을 메시지에 대한 스키마를 .proto 파일로 만든다음, protoc  protoc-gen-go 이용하여 .go 파일을 생성한다.

2) flag 패키지를 이용해 파라미터를 받고 명령 (add, list) 분기하여 실행할 있도록 해준다

3) add list  함수를 구현한다. proto.Marshal byte array 만들고, proto.Unmarshal todo.Task 만들어준다.

 

만들기

 


 

위와 같이 todo.proto 만들고, protoc --go_out=. todo.proto 라고 cli 명령을 내리면 코드가 생성됨

- Parse PROTO_FILES and generate output based on the options given

- todo.proto 파싱해서 현재 폴더 (.) output 생성하라는 명령임

 

만들어진 코드 일부.

우리가 만든 녀석들을 가져오는 함수가 자동으로 만들어진게 보인다


 

일단 main() 만들고 실행

 


 


 

 

기본 골격 구현

 



 

1) todo 치면 subcommand 치라고 안내

 

2) todo add todo list 치면 아무 동작도 없음

- 함수의 기능은 앞으로 구현할 예정

 

3) todo foo 치면 없는 subcommand 라고 나옴

 

본격 add, list 구현

 

func add()

 

1) proto.Marshal() 명령으로 task byte array 만든다.

2) os.OpenFile() dbPath 파일을 다음

3) 우선은 데이터의 길이정보를 4바이트 (little endian) 적고

4) f.Write(b) 나머지를 적어준다.

func list()

 

1) ioutil.ReadFile() 파일 전체를 읽는다.

2) 읽은 데이터가 없거나, 길이정보를 담은 4바이트에 못미치면 처리하고 리턴

3) binary.Read 함수로  첫번째 데이터의 길이를 확인한 다음

4) proto.Unmarshal()   byte array task 변환해준다.

5) task.Done, task.Text 뿌려준다

 

참고: fmt.Errorf

 

error 리턴해준다.

실제 print fmt.Println 해주는 것에 주목할



 

참고: fmt.Errorf fmt.Fprintln

 

io.Writer 인터페이스 에게 써준다.

 


 

참고. os.Exit(1)

 

- 참고링크: https://gobyexample.com/exit

- os.Exit() 나가면 defer 동작하지 않는다

- exit status 명시하고 싶을때에 별도로 넣어줘야 한다.

 


 

참고. binary.Write

 

1) io.Writer w 써준다.

2) 데이터는 고정된 크기의 값이나, 슬라이스, 또는 데이터를 가리키는 포인터이다.

- Boolean true 일때 1, false 일때 0으로 인코딩된다.

3) endian 인텔계열은 little endian, RISC 계열은 big endian 사용한다.

네트워크에서는 big endian 쓴다.

 

 


 

참고. f.Write

 

 

참고. ioutil.ReadFile

 

단순히 파일의 내용만을 일을때는 ioutil.ReadFile 쓰는 건이 간편하다.

많은 작업을 위해서는 os.Open 으로  열어서 File 리턴받아 다양한 작업이 가능하다.

 


 

 

 



반응형

'golang' 카테고리의 다른 글

Protocol Buffers - Golang Tutorial  (0) 2019.06.07
Protocol Buffers - overview  (1) 2019.06.06
Staircase Problem  (0) 2019.05.30
General Egg Problem  (0) 2019.05.24
Dynamic Programming  (0) 2019.05.22
반응형
잡학툰 뱃지
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함