[GO/gRPC/Protocol Buffer] message(struct) 생성/초기화 문법 대문자 이유

프로그래밍/Go lang2020. 11. 5. 21:10

 

// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
	log.Printf("Received: %v", in.GetName())
	return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
}

.proto 서비스에서 정의했던 interface를 .go파일에서 위와 같은 방식으로 선언을 하고 있습니다.

 

위 코드를 보면 이해가 잘 안가는 부분이 좀 있습니다.

return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil

 

1. Message 대문자 이유

.proto파일에서 인터페이스를 보면 HelloReply는 아래와 같이 정의되어 있습니다.

 

message HelloReply {
  string message = 1;
}

 

여기서 멤버?는 소문자로 선언되어 있습니다. 그런데 의아하게도.. 생성자로 보이는 부분에선 첫 글자를 대문자로 사용해서 접근하고 있네요.

 

그 이유는.. 제일 아래쪽 링크에 자세히 나와있습니다. Golang의 문법상 첫 글자가 대문자로 시작되어야 외부 패키지에서 접근이 가능하다고 합니다.

 

그래서 설령 .proto파일에서 소문자로 message를 사용했더라도 protoc에 의해 생성된 helloworld.pb.go를 보면 아래의 코드가 있습니다.

// The response message containing the greetings
type HelloReply struct {
    state         protoimpl.MessageState
    sizeCache     protoimpl.SizeCache
    unknownFields protoimpl.UnknownFields

    Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"`
}

 

소문자였던 message가 Message로 변경되어 있습니다.

그래서 그냥.. 간단히 규칙이 그렇구나라고 생각하면 되겠습니다.

 

 

2. return에서 , nil의 의미

왜 아래의 코드와 같이 return 구문에 , nil이 있는지 궁금했습니다.

return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil

 

보니까 return 인자가 2개로 정의된 상황에서 2번째 인자를 그냥 nil로 준 것이었네요.

 

함수 전체 코드를 보면 리턴의 인자가 2개입니다.

func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
    log.Printf("Received: %v", in.GetName())
    return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
}

 

 

 

참고

stackoverflow.com/questions/43716630/golang-using-lowercase-for-first-letter-in-struct-property

 

Golang using lowercase for first letter in Struct property

I have an issue with Golang view template, I currently using lowercase in the struct properties to build the struct then passed it to the view as a map. here is my Struct look like: type User str...

stackoverflow.com

developers.google.com/protocol-buffers/docs/reference/go-generated

 

Go Generated Code  |  Protocol Buffers  |  Google Developers

This page describes exactly what Go code the protocol buffer compiler generates for any given protocol definition. Any differences between proto2 and proto3 generated code are highlighted - note that these differences are in the generated code as described

developers.google.com

stackoverflow.com/questions/35983118/what-does-nil-mean-in-golang

 

What does nil mean in golang?

There are many cases using nil in golang. For example: func (u *URL) Parse(ref string) (*URL, error) { refurl, err := Parse(ref) if err != nil { return nil, err } return u.

stackoverflow.com

 

goland에서 *와 & 문법

gist.github.com/josephspurrier/7686b139f29601c3b370

 

Golang - Asterisk and Ampersand Cheatsheet

Golang - Asterisk and Ampersand Cheatsheet. GitHub Gist: instantly share code, notes, and snippets.

gist.github.com

 

작성자

Posted by 드리머즈

관련 글

댓글 영역