Merge remote-tracking branch 'nzoschke/bios-example'

This commit is contained in:
Mark McGranaghan 2018-02-01 16:31:09 -05:00
commit ca20eb066f
9 changed files with 28 additions and 25 deletions

View File

@ -7,7 +7,10 @@ git reset --hard $SHA
go get github.com/russross/blackfriday go get github.com/russross/blackfriday
PKGS=$(go list $PKG/... | grep -v examples) PKGS=$(go list $PKG/... | grep -v examples/variables)
run -s "Linting" golint -set_exit_status $PKGS run -s "Linting" golint -set_exit_status $PKGS
PKGS=$(go list $PKG/... | grep -v examples/panic)
run -s "Vetting" go vet -x $PKGS run -s "Vetting" go vet -x $PKGS
run -s "Building" tools/build run -s "Building" tools/build

View File

@ -15,7 +15,7 @@ func main() {
// We'll use an unsigned integer to represent our // We'll use an unsigned integer to represent our
// (always-positive) counter. // (always-positive) counter.
var ops uint64 = 0 var ops uint64
// To simulate concurrent updates, we'll start 50 // To simulate concurrent updates, we'll start 50
// goroutines that each increment the counter about // goroutines that each increment the counter about

View File

@ -14,7 +14,7 @@ import "fmt"
func intSeq() func() int { func intSeq() func() int {
i := 0 i := 0
return func() int { return func() int {
i += 1 i++
return i return i
} }
} }

View File

@ -21,7 +21,7 @@ package main
import "strings" import "strings"
import "fmt" import "fmt"
// Returns the first index of the target string `t`, or // Index returns the first index of the target string `t`, or
// -1 if no match is found. // -1 if no match is found.
func Index(vs []string, t string) int { func Index(vs []string, t string) int {
for i, v := range vs { for i, v := range vs {
@ -32,13 +32,13 @@ func Index(vs []string, t string) int {
return -1 return -1
} }
// Returns `true` if the target string t is in the // Include returns `true` if the target string t is in the
// slice. // slice.
func Include(vs []string, t string) bool { func Include(vs []string, t string) bool {
return Index(vs, t) >= 0 return Index(vs, t) >= 0
} }
// Returns `true` if one of the strings in the slice // Any returns `true` if one of the strings in the slice
// satisfies the predicate `f`. // satisfies the predicate `f`.
func Any(vs []string, f func(string) bool) bool { func Any(vs []string, f func(string) bool) bool {
for _, v := range vs { for _, v := range vs {
@ -49,7 +49,7 @@ func Any(vs []string, f func(string) bool) bool {
return false return false
} }
// Returns `true` if all of the strings in the slice // All returns `true` if all of the strings in the slice
// satisfy the predicate `f`. // satisfy the predicate `f`.
func All(vs []string, f func(string) bool) bool { func All(vs []string, f func(string) bool) bool {
for _, v := range vs { for _, v := range vs {
@ -60,7 +60,7 @@ func All(vs []string, f func(string) bool) bool {
return true return true
} }
// Returns a new slice containing all strings in the // Filter returns a new slice containing all strings in the
// slice that satisfy the predicate `f`. // slice that satisfy the predicate `f`.
func Filter(vs []string, f func(string) bool) []string { func Filter(vs []string, f func(string) bool) []string {
vsf := make([]string, 0) vsf := make([]string, 0)
@ -72,7 +72,7 @@ func Filter(vs []string, f func(string) bool) []string {
return vsf return vsf
} }
// Returns a new slice containing the results of applying // Map returns a new slice containing the results of applying
// the function `f` to each string in the original slice. // the function `f` to each string in the original slice.
func Map(vs []string, f func(string) string) []string { func Map(vs []string, f func(string) string) []string {
vsm := make([]string, len(vs)) vsm := make([]string, len(vs))

View File

@ -10,11 +10,11 @@ import "os"
// We'll use these two structs to demonstrate encoding and // We'll use these two structs to demonstrate encoding and
// decoding of custom types below. // decoding of custom types below.
type Response1 struct { type response1 struct {
Page int Page int
Fruits []string Fruits []string
} }
type Response2 struct { type response2 struct {
Page int `json:"page"` Page int `json:"page"`
Fruits []string `json:"fruits"` Fruits []string `json:"fruits"`
} }
@ -50,7 +50,7 @@ func main() {
// custom data types. It will only include exported // custom data types. It will only include exported
// fields in the encoded output and will by default // fields in the encoded output and will by default
// use those names as the JSON keys. // use those names as the JSON keys.
res1D := &Response1{ res1D := &response1{
Page: 1, Page: 1,
Fruits: []string{"apple", "peach", "pear"}} Fruits: []string{"apple", "peach", "pear"}}
res1B, _ := json.Marshal(res1D) res1B, _ := json.Marshal(res1D)
@ -60,7 +60,7 @@ func main() {
// to customize the encoded JSON key names. Check the // to customize the encoded JSON key names. Check the
// definition of `Response2` above to see an example // definition of `Response2` above to see an example
// of such tags. // of such tags.
res2D := &Response2{ res2D := &response2{
Page: 1, Page: 1,
Fruits: []string{"apple", "peach", "pear"}} Fruits: []string{"apple", "peach", "pear"}}
res2B, _ := json.Marshal(res2D) res2B, _ := json.Marshal(res2D)
@ -103,7 +103,7 @@ func main() {
// need for type assertions when accessing the decoded // need for type assertions when accessing the decoded
// data. // data.
str := `{"page": 1, "fruits": ["apple", "peach"]}` str := `{"page": 1, "fruits": ["apple", "peach"]}`
res := Response2{} res := response2{}
json.Unmarshal([]byte(str), &res) json.Unmarshal([]byte(str), &res)
fmt.Println(res) fmt.Println(res)
fmt.Println(res.Fruits[0]) fmt.Println(res.Fruits[0])

View File

@ -23,8 +23,8 @@ func main() {
// We'll keep track of how many read and write // We'll keep track of how many read and write
// operations we do. // operations we do.
var readOps uint64 = 0 var readOps uint64
var writeOps uint64 = 0 var writeOps uint64
// Here we start 100 goroutines to execute repeated // Here we start 100 goroutines to execute repeated
// reads against the state, once per millisecond in // reads against the state, once per millisecond in

View File

@ -13,7 +13,7 @@ import "fmt"
// corresponding type. Here we've created a `ByLength` // corresponding type. Here we've created a `ByLength`
// type that is just an alias for the builtin `[]string` // type that is just an alias for the builtin `[]string`
// type. // type.
type ByLength []string type byLength []string
// We implement `sort.Interface` - `Len`, `Less`, and // We implement `sort.Interface` - `Len`, `Less`, and
// `Swap` - on our type so we can use the `sort` package's // `Swap` - on our type so we can use the `sort` package's
@ -22,22 +22,22 @@ type ByLength []string
// hold the actual custom sorting logic. In our case we // hold the actual custom sorting logic. In our case we
// want to sort in order of increasing string length, so // want to sort in order of increasing string length, so
// we use `len(s[i])` and `len(s[j])` here. // we use `len(s[i])` and `len(s[j])` here.
func (s ByLength) Len() int { func (s byLength) Len() int {
return len(s) return len(s)
} }
func (s ByLength) Swap(i, j int) { func (s byLength) Swap(i, j int) {
s[i], s[j] = s[j], s[i] s[i], s[j] = s[j], s[i]
} }
func (s ByLength) Less(i, j int) bool { func (s byLength) Less(i, j int) bool {
return len(s[i]) < len(s[j]) return len(s[i]) < len(s[j])
} }
// With all of this in place, we can now implement our // With all of this in place, we can now implement our
// custom sort by casting the original `fruits` slice to // custom sort by casting the original `fruits` slice to
// `ByLength`, and then use `sort.Sort` on that typed // `byLength`, and then use `sort.Sort` on that typed
// slice. // slice.
func main() { func main() {
fruits := []string{"peach", "banana", "kiwi"} fruits := []string{"peach", "banana", "kiwi"}
sort.Sort(ByLength(fruits)) sort.Sort(byLength(fruits))
fmt.Println(fruits) fmt.Println(fruits)
} }

View File

@ -37,8 +37,8 @@ type writeOp struct {
func main() { func main() {
// As before we'll count how many operations we perform. // As before we'll count how many operations we perform.
var readOps uint64 = 0 var readOps uint64
var writeOps uint64 = 0 var writeOps uint64
// The `reads` and `writes` channels will be used by // The `reads` and `writes` channels will be used by
// other goroutines to issue read and write requests, // other goroutines to issue read and write requests,

View File

@ -9,7 +9,7 @@ import "fmt"
func main() { func main() {
// `var` declares 1 or more variables. // `var` declares 1 or more variables.
var a string = "initial" var a = "initial"
fmt.Println(a) fmt.Println(a)
// You can declare multiple variables at once. // You can declare multiple variables at once.