Code Snippets Go

Short texts Go 🧑‍💻 Code snippets Go ✍️ Reading lists Go 👀

fifo.go

package main import ( “bufio” “context” “encoding/binary” “fmt” “os” “strings” “syscall” “github.com/containerd/fifo” ) func usage() { fmt.Printf(“%s \n”, os.Args[0]) os.Exit(1) } func parseFlags(flagStr string) int { var flags int fields := strings.Split(strings.ToLower(flagStr), “,”) for _, f := range fields { switch f { case “creat”: flags |= syscall.O_CREAT case “rdonly”: flags |= syscall.O_RDONLY case “wronly”: […]

invoice-dynamodb-storage.go

// API is a subset of DynamoDB API type API interface { GetItem(*dynamodb.GetItemInput) (*dynamodb.GetItemOutput, error) PutItem(*dynamodb.PutItemInput) (*dynamodb.PutItemOutput, error) } type Dynamo struct { client API // DynamoDB API client table string } // make sure Dynamo implements invoice.Storage interface var _ invoice.Storage = (*Dynamo)(nil) func (d *Dynamo) AddInvoice(inv invoice.Invoice) error {} func (d *Dynamo) FindInvoice(id […]

arm64v8 Dockerfile example for Intel architecture

ARG VERSION=stable-slim FROM arm64v8/debian:${VERSION} RUN export DEBIAN_FRONTEND=noninteractive && \ apt update && \ apt -y install build-essential git golang-go RUN uname -a ARG PROJECT=goarm WORKDIR /workspaces/${PROJECT} COPY hello.go . CMD go run hello.go package main import ( “fmt” ) func main() { fmt.Println(“hello emu!”) }

server file implementing the server functionality

package servers import ( “context” “fmt” “io” “strconv” “github.com/prateekgupta3991/blockchain-experiment/base” rpc “github.com/prateekgupta3991/vault/rpc/gen” ) type Srvr struct { rpc.UnimplementedNodeOpsServer Primary *base.Node } func (s *Srvr) FindDestNode(ctx context.Context, k *rpc.KeyDetails) (*rpc.DestinationNodeDetails, error) { fmt.Println(“This server works”) return &rpc.DestinationNodeDetails{ Nid: “0”, }, nil } func (s *Srvr) InsertNodes(stream rpc.NodeOps_InsertNodesServer) error { fmt.Println(“InsertNodes Function”) for { req, err := stream.Recv() […]