Skip to content

Instantly share code, notes, and snippets.

@jasonsalas
jasonsalas / main.rs
Created November 20, 2023 23:32
Implementing printable enums in Rust
/*
Sports player profiles in Rust 1.73.0
--------------------------------------
This code demo highlights how to:
- implement the fmt::Display trait for enums (Line 21)
- develop a helper method to capitalize the first letter of words (Line 56)
- apply turbofish type casting (Line 60)
*/
use std::fmt;
@jasonsalas
jasonsalas / README.md
Last active July 1, 2023 08:50
Kafka-based pub/sub demo in Chapter 6 of "Microservices in Go" by Alexander Shuiskov

Running Chapter 6's pub/sub demo using Kafka

Here's how to setup Kafka as a pub/sub engine for Alexander Shuiskov's excellent Microservices with Go, and how to create a producer and consumer for your instance in a CLI

running Docker Compose

run Kafka in a Docker container (from movieapp/src/cmd/ratingingester):

  • docker-compose -d up
  • docker exec -it kafka /bin/sh
@jasonsalas
jasonsalas / quicksort.go
Created July 14, 2022 00:04
QuickSort algorithm in Go
package api
import "math/rand"
// QuickSort is a divide-&-conquer algorithm running in O(n log n) time that receives an integer slice
// and returns a sorted slice of integers (h/t: https://gist.github.com/vderyagin/4161347)
func QuickSort(numbers []int) []int {
length := len(numbers)
/* BASE CASE */
@jasonsalas
jasonsalas / main.go
Created January 9, 2022 01:03
Performance-testing loop types in bubble sort algorithm in Go (for/each vs. while vs. infinite loops)
// simple sorting algorithms
package main
import "fmt"
func getNumbers() []int {
return []int{12, 7, 256, 4, 22, 44, 55, 85, 56, 3, 6, 1, 98, 76}
}
func RangeLoopSort(arr []int) []int {
@jasonsalas
jasonsalas / pkg-server.go
Created August 16, 2021 19:10
gRPC server-side streaming in Go - server package
package transaction
import (
"log"
"math/rand"
"time"
"github.com/golang/protobuf/ptypes"
bank "github.com/jasonsalas/protobank/pkg/protobuf/bank"
"google.golang.org/grpc/codes"
@jasonsalas
jasonsalas / pkg-client.go
Created August 16, 2021 19:07
gRPC server-side streaming in Go - client package
package transaction
import (
"context"
"io"
"log"
bank "github.com/jasonsalas/protobank/pkg/protobuf/bank"
"google.golang.org/grpc"
)
@jasonsalas
jasonsalas / transaction.proto
Created August 16, 2021 19:02
gRPC server-side streaming in Go - service definition
syntax = "proto3";
package bank;
option go_package = "github.com/jasonsalas/protobank";
import "google/protobuf/timestamp.proto";
message Transaction {
enum Operation {
@jasonsalas
jasonsalas / client.go
Created August 16, 2021 18:59
gRPC server-side streaming in Go - client application
package main
import (
"context"
"fmt"
"log"
"github.com/jasonsalas/protobank/internal/transaction"
"google.golang.org/grpc"
)
@jasonsalas
jasonsalas / server.go
Created August 16, 2021 18:58
gRPC server-side streaming in Go - server application
package main
import (
"fmt"
"log"
"net"
"github.com/jasonsalas/protobank/internal/transaction"
bank "github.com/jasonsalas/protobank/pkg/protobuf/bank"
"google.golang.org/grpc"
@jasonsalas
jasonsalas / workerpools.go
Last active July 5, 2020 07:07
Using worker pools & buffered channels in Go
/* using worker pools & buffered channels in Go */
// h/t: Naveen Ramanathan @ golangbot.com
// https://golangbot.com/buffered-channels-worker-pools/
package main
import (
"fmt"
"math/rand"
"sync"