dont export types (golint)
This commit is contained in:
parent
ee20cfb543
commit
ee5400dc9f
@ -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])
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user