Skip to content
:tech
← Handbook

AI-translated. Read the original in Korean

Go Projects

Starting a Project

First, create a new project by following Starting a Project.

Installing Dependencies

Use dep to initialize the required dependencies.

dep init

When dependencies change, update them with dep ensure.

Project Layout

The Go project layout generally follows the definitions in golang-standards/project-layout.

DirectoryDescription
/cmdContains the project’s main applications. Name each directory under /cmd after its executable. These applications start servers, establish database connections, and perform similar setup tasks.
/internalContains library code that is not exposed outside the project. Most code in a service project lives here.
/internal/appContains the actual application code. The presentation layer, including frameworks, generally lives here.
/internal/pkgContains library code shared between applications. The domain and data layers generally live here.
/pkgContains publicly exposed library code.
/vendorContains external libraries used by the project, usually managed with dep.

Implementing an API

Implement the functionality of the API defined using API Definition.

Adding the Library

The Go library built from an API definition is generated in the go directory of buzzapis. Use a dependency tool such as Dep to retrieve the API libraries you need.

  1. Import the API library.

    // e.g main.go
    import "github.com/Buzzvil/buzzapis/go/library"
    
  2. Use Dep to retrieve the library.

    $ dep ensure
    

Implementing the Interface

Implement the API defined in the library to provide the service to clients.

package librarysrv

import (
    pb "github.com/Buzzvil/buzzapis/go/library"
)

type server struct {
}

// New creates library service server.
func New(db *sql.DB) pb.LibraryServiceServer {
    return &server{}
}

func (s *server) GetBook(c context.Context, r *pb.GetBookRequest) (*pb.Book, error) {
    // TODO: Find the book from database.
    return &pb.Book{Title: "Title here.", Author: "Author here."}, nil
}

Setting Up the Server

Creating and Running the Server

Use gRPC to create and run the server. Keep main.go focused on establishing connections and starting the server, delegating the actual implementation to internal packages. For details, see Starting the go server.

// cmd/librarysvc/main.go
package librarysvc

import (
    "log"
    "net"
    "os"

    pb "github.com/Buzzvil/buzzapis/go/library"
    "github.com/Buzzvil/librarysvc/internal/app/librarysrv"
    "google.golang.org/grpc"
)

func main() {
    lis, err := net.Listen("tcp", fmt.Sprintf(":%s", os.Getenv("PORT")))
    if err != nil {
        log.Fatalf("Failed to listen: %v", err)
    }
    s := grpc.NewServer()
    pb.RegisterLibraryServiceServer(s, librarysrv.New())
    if err := s.Serve(lis); err != nil {
        log.Fatalf("Failed to serve: %v", err)
    }
}

Server Logging

Configure logging options using a library such as logrus. See the logrus repository for usage details.

// cmd/librarysvc/main.go
package librarysvc

import (
    "github.com/grpc-ecosystem/go-grpc-middleware"
    "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
    "github.com/grpc-ecosystem/go-grpc-middleware/tags"
    "github.com/sirupsen/logrus"
)

func newGrpcServer() *grpc.Server {
    logrusEntry := logrus.NewEntry(logrus.StandardLogger())
    opts := []grpc_logrus.Option{}
    grpc_logrus.ReplaceGrpcLogger(logrusEntry)
    return grpc.NewServer(
        grpc_middleware.WithUnaryServerChain(
            grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
            grpc_logrus.UnaryServerInterceptor(logrusEntry, opts...),
        ),
        grpc_middleware.WithStreamServerChain(
            grpc_ctxtags.StreamServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
            grpc_logrus.StreamServerInterceptor(logrusEntry, opts...),
        ),
    )
}
All guides
Handbook

API Definition

This document explains how to define and use APIs (application programming interfaces) at Buzzvil. Interfaces are defined using Protocol Buffers. Guidelines API-First Approach Use…

Handbook

Buzzvil Architecture

This website brings together an overview of Buzzvil’s software architecture and the documentation needed to develop within it. Introduction The advertising ecosystem is complex bec…

Handbook

Service Deployment

This document describes what you need to prepare to deploy backend services at Buzzvil. Guidelines The process for deploying services to Kubernetes at Buzzvil is shown below. Altho…