brew install go
go mod init github.com/bossm0n5t3r/learning-go
Module
설정을 해두어야 한다.
go test ./...
go get github.com/gin-gonic/gin
이 경우, go.mod
파일이 수정된다.
# 기존 go.mod 파일
module github.com/bossm0n5t3r/learning-go
go 1.16
# go get 후 go.mod 파일
module github.com/bossm0n5t3r/learning-go
go 1.16
require github.com/gin-gonic/gin v1.7.2 // indirect
~/go
안에 아래 위치에 모듈이 추가된다.
zhoon 🚀 ~
cd go/pkg/mod/github.com
zhoon 🚀 ~/go/pkg/mod/github.com
ll
total 0
drwxr-xr-x 3 matt staff 96B 5 28 18:15 !burnt!sushi
drwxr-xr-x 3 matt staff 96B 6 1 18:40 cosiner
drwxr-xr-x 3 matt staff 96B 6 1 18:40 cpuguy83
drwxr-xr-x 3 matt staff 96B 6 1 01:01 cweill
drwxr-xr-x 5 matt staff 160B 6 1 18:40 fatih
drwxr-xr-x 3 matt staff 96B 6 2 14:48 gin-contrib
drwxr-xr-x 3 matt staff 96B 6 2 14:48 gin-gonic
drwxr-xr-x 4 matt staff 128B 6 1 18:40 go-delve
drwxr-xr-x 5 matt staff 160B 6 2 14:48 go-playground
drwxr-xr-x 3 matt staff 96B 6 2 14:48 golang
drwxr-xr-x 4 matt staff 128B 6 1 18:40 google
drwxr-xr-x 3 matt staff 96B 6 1 18:40 hashicorp
drwxr-xr-x 3 matt staff 96B 6 1 18:40 haya14busa
drwxr-xr-x 3 matt staff 96B 6 1 18:40 inconshreveable
drwxr-xr-x 3 matt staff 96B 6 1 18:40 josharian
drwxr-xr-x 3 matt staff 96B 6 2 14:48 json-iterator
drwxr-xr-x 3 matt staff 96B 6 1 18:40 karrick
drwxr-xr-x 3 matt staff 96B 6 1 18:40 konsorten
drwxr-xr-x 3 matt staff 96B 6 2 14:48 leodido
drwxr-xr-x 5 matt staff 160B 6 2 14:48 mattn
drwxr-xr-x 4 matt staff 128B 6 2 14:48 modern-go
drwxr-xr-x 3 matt staff 96B 6 1 18:40 peterh
drwxr-xr-x 3 matt staff 96B 6 1 18:40 pkg
drwxr-xr-x 3 matt staff 96B 6 1 18:40 ramya-rao-a
drwxr-xr-x 4 matt staff 128B 6 1 18:40 russross
drwxr-xr-x 3 matt staff 96B 5 28 18:15 sergi
drwxr-xr-x 3 matt staff 96B 6 1 18:40 sirupsen
drwxr-xr-x 3 matt staff 96B 6 1 18:40 skratchdot
drwxr-xr-x 4 matt staff 128B 6 1 18:40 spf13
drwxr-xr-x 4 matt staff 128B 6 2 14:48 ugorji
drwxr-xr-x 5 matt staff 160B 6 1 18:40 uudashr
go mod tidy
go.mod
의 require
도 삭제된다./cmd
/pkg
/internal
/assets
/internal
와 /pkg
디렉터리 코드를 임포트, 호출만 하는 작은 main
함수는 흔히 볼 수 있다./pkg
/internal
~/.zshrc
에 다음을 추가해주면 된다.# GOPATH
export GOPATH=$HOME/go
export PATH="$PATH:$GOPATH/bin"
znly
라는 어플에서 만들어서 사용하는 도커라고 한다.
100K+
정도니 많이들 사용하는 모양이다.protoc-gen-go-grpc
가 없기 때문에 문제가 발생한다.
docker run --rm -v $(pwd):$(pwd) -w $(pwd) znly/protoc -I. --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative helloworld/helloworld.proto
protoc-gen-go-grpc: program not found or is not executable
--go-grpc_out: protoc-gen-go-grpc: Plugin failed with status code 1.
$ 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
@latest
또는 버전을 꼭 명시해줘야한다. 이를 명시하지 않으면 다음과 같은 에러가 발생한다.
go install google.golang.org/protobuf/cmd/protoc-gen-go
go install: version is required when current directory is not in a module
Try 'go install google.golang.org/protobuf/cmd/protoc-gen-go@latest' to install the latest version
Prerequisites
를 보고 미리 준비해보자.
api
폴더 아래에 helloworld/helloworld.proto
라는 파일을 만들자.
option go_package =
뒤에만 본인 패키지를 적으면 되는 것 같다.
option go_package = "github.com/bossm0n5t3r/learning-go/api/helloworld";
이렇게 수정했다.Regenerate gRPC code
를 따라하면 된다.
./api/helloworld/helloworld.proto
로 하면 작업하는 루트 디렉터리에서도 잘 된다.helloworld.pb.go
, helloworld_grpc.pb.go
라는 두 개의 파일이 생성된다.cmd/grpc_server/main.go
파일을 https://github.com/grpc/grpc-go/blob/master/examples/helloworld/greeter_server/main.go 내용으로 생성하자.
pb "github.com/bossm0n5t3r/learning-go/api/helloworld"
정도이다."google.golang.org/grpc"
를 설치하지 않았다면, 아래 명령어로 설치해주자.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
helloworld.pb.go
, helloworld_grpc.pb.go
도 멀쩡히 잘 생성되었다면, 아래 명렁어를 통해서 서버를 실행하자. go run cmd/grpc_server/main.go
2021/06/15 01:00:03 server listening at [::]:50051