go language backend playground
create package
go mod init github.com/stkali/garden
install
go get -u github.com/spf13/cobra@latest
go get -u github.com/spf13/cobra-cli@latest
create app structure use cobra and cobra-cli
cobra-cli init -a "<st·kali [email protected]>" -l MIT --viper
cobra-cli add config -a "<st·kali [email protected]>" -l MIT
cobra-cli add server -a "<st·kali [email protected]>" -l MIT
import golang migration
migrate create -ext sql -dir db/migrations -seq desc_table
sqlc generate
sqlc config file sample
version: 1
packages:
- path: "db/sqlc"
name: "db"
engine: "postgresql"
schema: "db/migration/"
queries: "db/query"
emit_json_tags: true
emit_prepared_queries: false
emit_interface: true
emit_exact_table_names: false
emit_empty_slices: true
genreate go code from sql query
sqlc generate
install proto buffer compiler
install Golang plugins for the protocol compiler:
go install google.golang.org/protobuf/cmd/[email protected]
go install google.golang.org/grpc/cmd/[email protected]
Update your PATH so that the protoc compiler can find the plugins:
export PATH="$PATH:$(go env GOPATH)/bin"
# or write it to profile or zshrc file
evans is a gRPC client
# regsiter reflection
reflection.Register(grpcServer)
connect rpc server
evans -r repl --host <server address> --port <server port>
generate http server by protobuf and grpc 🏠 grpc-ecosystem/grpc-gateway
1 copy *.proto to project
// +build tools
package tools
import (
_ "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway"
_ "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2"
_ "google.golang.org/grpc/cmd/protoc-gen-go-grpc"
_ "google.golang.org/protobuf/cmd/protoc-gen-go"
)
# pull dependencies
go mod tidy
# install binaries
go install \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2 \
google.golang.org/protobuf/cmd/protoc-gen-go \
google.golang.org/grpc/cmd/protoc-gen-go-grpc
add file to $PROJECT/proto/google/api/*.proto
google/api/annotations.proto
google/api/field_behavior.proto
google/api/http.proto
google/api/httpbody.proto