TIL

Go


설치하기

Mac

brew install go

VSCode extension 설치

go mod init

go mod init github.com/bossm0n5t3r/learning-go

전체 테스트 코드 실행

go test ./...

Module

추가하기

모듈이 추가되는 위치

사용하지 않는 모듈 삭제하기

프로젝트 레이아웃

Go 디렉터리

/cmd

GOPATH, PATH 설정하기

# GOPATH
export GOPATH=$HOME/go
export PATH="$PATH:$GOPATH/bin"

프로토콜 컴파일러 플러그인 설치하기

도커로 설치해서 사용하기

$ docker pull znly/protoc
 docker run --rm znly/protoc --help
Usage: /usr/bin/protoc [OPTION] PROTO_FILES
Parse PROTO_FILES and generate output based on the options given:
-IPATH, --proto_path=PATH   Specify the directory in which to search for
                            imports.  May be specified multiple times;
                            directories will be searched in order.  If not
                            given, the current working directory is used.
--version                   Show version info and exit.
-h, --help                  Show this text and exit.
--encode=MESSAGE_TYPE       Read a text-format message of the given type
                            from standard input and write it in binary
                            to standard output.  The message type must
                            be defined in PROTO_FILES or their imports.
--decode=MESSAGE_TYPE       Read a binary message of the given type from
                            standard input and write it in text format
                            to standard output.  The message type must
                            be defined in PROTO_FILES or their imports.
--decode_raw                Read an arbitrary protocol message from
                            standard input and write the raw tag/value
                            pairs in text format to standard output.  No
                            PROTO_FILES should be given when using this
                            flag.
--descriptor_set_in=FILES   Specifies a delimited list of FILES
                            each containing a FileDescriptorSet (a
                            protocol buffer defined in descriptor.proto).
                            The FileDescriptor for each of the PROTO_FILES
                            provided will be loaded from these
                            FileDescriptorSets. If a FileDescriptor
                            appears multiple times, the first occurrence
                            will be used.
-oFILE,                     Writes a FileDescriptorSet (a protocol buffer,
  --descriptor_set_out=FILE defined in descriptor.proto) containing all of
                            the input files to FILE.
--include_imports           When using --descriptor_set_out, also include
                            all dependencies of the input files in the
                            set, so that the set is self-contained.
--include_source_info       When using --descriptor_set_out, do not strip
                            SourceCodeInfo from the FileDescriptorProto.
                            This results in vastly larger descriptors that
                            include information about the original
                            location of each decl in the source file as
                            well as surrounding comments.
--dependency_out=FILE       Write a dependency output file in the format
                            expected by make. This writes the transitive
                            set of input file paths to FILE
--error_format=FORMAT       Set the format in which to print errors.
                            FORMAT may be 'gcc' (the default) or 'msvs'
                            (Microsoft Visual Studio format).
--print_free_field_numbers  Print the free field numbers of the messages
                            defined in the given proto files. Groups share
                            the same field number space with the parent
                            message. Extension ranges are counted as
                            occupied fields numbers.

--plugin=EXECUTABLE         Specifies a plugin executable to use.
                            Normally, protoc searches the PATH for
                            plugins, but you may specify additional
                            executables not in the path using this flag.
                            Additionally, EXECUTABLE may be of the form
                            NAME=PATH, in which case the given plugin name
                            is mapped to the given executable even if
                            the executable's own name differs.
--cpp_out=OUT_DIR           Generate C++ header and source.
--csharp_out=OUT_DIR        Generate C# source file.
--java_out=OUT_DIR          Generate Java source file.
--javanano_out=OUT_DIR      Generate Java Nano source file.
--js_out=OUT_DIR            Generate JavaScript source.
--objc_out=OUT_DIR          Generate Objective C header and source.
--php_out=OUT_DIR           Generate PHP source file.
--python_out=OUT_DIR        Generate Python source file.
--ruby_out=OUT_DIR          Generate Ruby source file.

직접 설치하기

 go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go: downloading google.golang.org/protobuf v1.26.0

 go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
go: downloading google.golang.org/grpc v1.38.0
go: downloading google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0
go: downloading google.golang.org/protobuf v1.23.0

간단한 grpc-server 띄워보기

go get -u google.golang.org/grpc
go get google.golang.org/protobuf/reflect/protoreflect@v1.26.0
go get google.golang.org/protobuf/runtime/protoimpl@v1.26.0
 go run cmd/grpc_server/main.go
2021/06/15 01:00:03 server listening at [::]:50051