This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Convert from Go structure to binary data | |
b := protobuf.Book{ | |
Name: "1984", | |
Author: "George Orwell", | |
Genre: "Dystopian", | |
Rating: 10, | |
} | |
binBook, err := proto.Marshal(&b) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/songx23/road-to-go-pro-example/equation" | |
) | |
func main() { | |
linearAnswer := equation.SolveLinear(1, 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module github.com/songx23/road-to-go-pro-example/sample | |
go 1.18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SolveLinear solves linear equation in the format of ax + b = 0 | |
func SolveLinear(a, b float64) float64 { | |
return -b / a | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module github.com/songx23/road-to-go-pro-example/equation | |
go 1.18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func workingEqual(param1, param2 []byte) bool { | |
if len(param1) != len(param2) { | |
return false | |
} | |
for i := range param1 { | |
if param1[i] != param2[i] { | |
return false | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func Test_faultyEqual(t *testing.T) { | |
type args struct { | |
param1 []byte | |
param2 []byte | |
} | |
tests := []struct { | |
name string | |
args args | |
want bool | |
}{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func faultyEqual(param1, param2 []byte) bool { | |
for i := range param1 { | |
if param1[i] != param2[i] { | |
return false | |
} | |
} | |
return true | |
} |
NewerOlder