Skip to content

Instantly share code, notes, and snippets.

View songx23's full-sized avatar

Song Xue songx23

View GitHub Profile
@songx23
songx23 / go-protobuf-conversion-example.go
Created July 6, 2022 05:29
Conversion between Go structure and binary data using Protobuf
// Convert from Go structure to binary data
b := protobuf.Book{
Name: "1984",
Author: "George Orwell",
Genre: "Dystopian",
Rating: 10,
}
binBook, err := proto.Marshal(&b)
@songx23
songx23 / goavro-tobinary-example.go
Created July 6, 2022 05:13
Example code to convert Go structure to binary using Avro codec
type Book struct {
Name string `json:"Name" avro:"Name" mapstructure:"Name"`
Author string `json:"Author" avro:"Author" mapstructure:"Author"`
Genre string `json:"Genre" avro:"Genre" mapstructure:"Genre"`
Rating int `json:"Rating" avro:"Rating" mapstructure:"Rating"`
}
func (b *Book) ToBinary(schema string) ([]byte, error) {
j, err := json.Marshal(b)
if err != nil {
@songx23
songx23 / sample-main.go
Created May 20, 2022 13:07
Sample main function that uses the equation module
package main
import (
"fmt"
"github.com/songx23/road-to-go-pro-example/equation"
)
func main() {
linearAnswer := equation.SolveLinear(1, 2)
@songx23
songx23 / go.mod
Created May 20, 2022 13:01
Sample workspace sample go module file
module github.com/songx23/road-to-go-pro-example/sample
go 1.18
@songx23
songx23 / rtgp-workspace-linear-eq.go
Created May 20, 2022 12:38
Sample equation function - linear
// SolveLinear solves linear equation in the format of ax + b = 0
func SolveLinear(a, b float64) float64 {
return -b / a
}
@songx23
songx23 / go.mod
Created May 20, 2022 12:34
Sample workspace equation go module file
module github.com/songx23/road-to-go-pro-example/equation
go 1.18
@songx23
songx23 / rtgp-spc2-working-equal.go
Created March 29, 2022 12:21
Working equal function
func workingEqual(param1, param2 []byte) bool {
if len(param1) != len(param2) {
return false
}
for i := range param1 {
if param1[i] != param2[i] {
return false
}
}
@songx23
songx23 / rtgp-spc2-fuzz-faulty-equal.go
Created March 29, 2022 12:03
Fuzzing test for the faulty equal function
func FuzzFaultyEqual(f *testing.F) {
f.Add([]byte("abc"), []byte("abc"))
f.Fuzz(func(t *testing.T, param1, param2 []byte) {
e := faultyEqual(param1, param2)
if e != (string(param1) == string(param2)) {
t.Errorf("equal failed. result: %t, param1: %s, param2: %s", e, param1, param2)
}
})
}
@songx23
songx23 / rtgp-spc2-unit-test-faulty-equal.go
Created March 29, 2022 11:59
Unit test for the faulty equal function
func Test_faultyEqual(t *testing.T) {
type args struct {
param1 []byte
param2 []byte
}
tests := []struct {
name string
args args
want bool
}{
@songx23
songx23 / rtgp-spc2-fault-equal.go
Created March 29, 2022 11:54
Faulty equal function for byte slices
func faultyEqual(param1, param2 []byte) bool {
for i := range param1 {
if param1[i] != param2[i] {
return false
}
}
return true
}