refactor: common ifaces
This commit is contained in:
29
interfaces/09-little-refactor/list.go
Normal file
29
interfaces/09-little-refactor/list.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type list []*product
|
||||||
|
|
||||||
|
func (l list) print() {
|
||||||
|
if len(l) == 0 {
|
||||||
|
fmt.Println("Sorry. We're waiting for delivery 🚚.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, p := range l {
|
||||||
|
p.print()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l list) discount(ratio float64) {
|
||||||
|
for _, p := range l {
|
||||||
|
p.discount(ratio)
|
||||||
|
}
|
||||||
|
}
|
19
interfaces/09-little-refactor/main.go
Normal file
19
interfaces/09-little-refactor/main.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
l := list{
|
||||||
|
{title: "moby dick", price: 10, released: toTimestamp(118281600)},
|
||||||
|
{title: "odyssey", price: 15, released: toTimestamp("733622400")},
|
||||||
|
{title: "hobbit", price: 25},
|
||||||
|
}
|
||||||
|
|
||||||
|
l.discount(.5)
|
||||||
|
l.print()
|
||||||
|
}
|
@ -7,6 +7,10 @@
|
|||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
type game struct {
|
import "fmt"
|
||||||
product
|
|
||||||
|
type money float64
|
||||||
|
|
||||||
|
func (m money) string() string {
|
||||||
|
return fmt.Sprintf("$%.2f", m)
|
||||||
}
|
}
|
26
interfaces/09-little-refactor/product.go
Normal file
26
interfaces/09-little-refactor/product.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type product struct {
|
||||||
|
title string
|
||||||
|
price money
|
||||||
|
released timestamp
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *product) print() {
|
||||||
|
fmt.Printf("%s: %s (%s)\n", p.title, p.price.string(), p.released.string())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *product) discount(ratio float64) {
|
||||||
|
p.price *= money(1 - ratio)
|
||||||
|
}
|
45
interfaces/09-little-refactor/timestamp.go
Normal file
45
interfaces/09-little-refactor/timestamp.go
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// timestamp stores, formats and automatically prints a timestamp.
|
||||||
|
type timestamp struct {
|
||||||
|
// timestamp anonymously embeds a time.
|
||||||
|
// no need to convert a time value to a timestamp value to use the methods of the time type.
|
||||||
|
time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ts timestamp) string() string {
|
||||||
|
if ts.IsZero() { // same as: ts.Time.IsZero()
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mon Jan 2 15:04:05 -0700 MST 2006
|
||||||
|
const layout = "2006/01"
|
||||||
|
return ts.Format(layout) // same as: ts.Time.Format(layout)
|
||||||
|
}
|
||||||
|
|
||||||
|
// toTimestamp returns a timestamp value depending on the type of `v`.
|
||||||
|
func toTimestamp(v interface{}) (ts timestamp) {
|
||||||
|
var t int
|
||||||
|
|
||||||
|
switch v := v.(type) {
|
||||||
|
case int:
|
||||||
|
t = v
|
||||||
|
case string:
|
||||||
|
t, _ = strconv.Atoi(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
ts.Time = time.Unix(int64(t), 0)
|
||||||
|
return ts
|
||||||
|
}
|
33
interfaces/10-stringer/list.go
Normal file
33
interfaces/10-stringer/list.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type list []*product
|
||||||
|
|
||||||
|
func (l list) String() string {
|
||||||
|
if len(l) == 0 {
|
||||||
|
return "Sorry. We're waiting for delivery 🚚.\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
var str strings.Builder
|
||||||
|
for _, p := range l {
|
||||||
|
fmt.Fprintf(&str, "* %s\n", p)
|
||||||
|
}
|
||||||
|
return str.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l list) discount(ratio float64) {
|
||||||
|
for _, p := range l {
|
||||||
|
p.discount(ratio)
|
||||||
|
}
|
||||||
|
}
|
32
interfaces/10-stringer/main.go
Normal file
32
interfaces/10-stringer/main.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// The money type is a stringer.
|
||||||
|
// You don't need to call the String method when printing a value of it.
|
||||||
|
// var pocket money = 10
|
||||||
|
// fmt.Println(pocket)
|
||||||
|
|
||||||
|
l := list{
|
||||||
|
{title: "moby dick", price: 10, released: toTimestamp(118281600)},
|
||||||
|
{title: "odyssey", price: 15, released: toTimestamp("733622400")},
|
||||||
|
{title: "hobbit", price: 25},
|
||||||
|
}
|
||||||
|
|
||||||
|
// The list is a stringer.
|
||||||
|
// The `fmt.Print` function can print the `l`
|
||||||
|
// by calling `l`'s `String()` method.
|
||||||
|
//
|
||||||
|
// Underneath, `fmt.Print` uses a type switch to
|
||||||
|
// detect whether a type is a Stringer:
|
||||||
|
// https://golang.org/src/fmt/print.go#L627
|
||||||
|
fmt.Print(l)
|
||||||
|
}
|
@ -7,21 +7,20 @@
|
|||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
type product struct {
|
type product struct {
|
||||||
Title string
|
title string
|
||||||
Price money
|
price money
|
||||||
}
|
released timestamp
|
||||||
|
|
||||||
func (p *product) discount(ratio float64) {
|
|
||||||
p.Price *= money(1 - ratio)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *product) sum() money {
|
|
||||||
return p.Price
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *product) String() string {
|
func (p *product) String() string {
|
||||||
return fmt.Sprintf("%-15s: %s", p.Title, p.Price)
|
return fmt.Sprintf("%s: %s (%s)", p.title, p.price, p.released)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *product) discount(ratio float64) {
|
||||||
|
p.price *= money(1 - ratio)
|
||||||
}
|
}
|
43
interfaces/10-stringer/timestamp.go
Normal file
43
interfaces/10-stringer/timestamp.go
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// timestamp stores, formats and automatically prints a timestamp: it's a stringer.
|
||||||
|
type timestamp struct {
|
||||||
|
time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// String method makes the timestamp an fmt.stringer.
|
||||||
|
func (ts timestamp) String() string {
|
||||||
|
if ts.IsZero() {
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mon Jan 2 15:04:05 -0700 MST 2006
|
||||||
|
const layout = "2006/01"
|
||||||
|
return ts.Format(layout)
|
||||||
|
}
|
||||||
|
|
||||||
|
func toTimestamp(v interface{}) (ts timestamp) {
|
||||||
|
var t int
|
||||||
|
|
||||||
|
switch v := v.(type) {
|
||||||
|
case int:
|
||||||
|
t = v
|
||||||
|
case string:
|
||||||
|
t, _ = strconv.Atoi(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
ts.Time = time.Unix(int64(t), 0)
|
||||||
|
return ts
|
||||||
|
}
|
54
interfaces/11-sort/list.go
Normal file
54
interfaces/11-sort/list.go
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// product is now *product because sorting will unnecessarily copy the product values
|
||||||
|
|
||||||
|
type list []*product
|
||||||
|
|
||||||
|
func (l list) String() string {
|
||||||
|
if len(l) == 0 {
|
||||||
|
return "Sorry. We're waiting for delivery 🚚.\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Sort(l)
|
||||||
|
var str strings.Builder
|
||||||
|
for _, p := range l {
|
||||||
|
fmt.Fprintf(&str, "* %s\n", p)
|
||||||
|
}
|
||||||
|
return str.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l list) discount(ratio float64) {
|
||||||
|
for _, p := range l {
|
||||||
|
p.discount(ratio)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// by default `list` sorts by `title`.
|
||||||
|
func (l list) Len() int { return len(l) }
|
||||||
|
func (l list) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
|
||||||
|
func (l list) Less(i, j int) bool { return l[i].title < l[j].title }
|
||||||
|
|
||||||
|
// byRelease sorts by product release dates.
|
||||||
|
type byRelease struct {
|
||||||
|
// byRelease embeds `list` and reuses list's Len and Swap methods.
|
||||||
|
list
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bp byRelease) Less(i, j int) bool {
|
||||||
|
// Before() accepts a time.Time but `released` is not time.Time.
|
||||||
|
// `released.Time` is necessary.
|
||||||
|
return bp.list[i].released.Before(bp.list[j].released.Time)
|
||||||
|
}
|
28
interfaces/11-sort/main.go
Normal file
28
interfaces/11-sort/main.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
l := list{
|
||||||
|
{title: "moby dick", price: 10, released: toTimestamp(118281600)},
|
||||||
|
{title: "odyssey", price: 15, released: toTimestamp("733622400")},
|
||||||
|
{title: "hobbit", price: 25},
|
||||||
|
}
|
||||||
|
|
||||||
|
// sort.Sort(l)
|
||||||
|
// sort.Sort(sort.Reverse(l))
|
||||||
|
// sort.Sort(byRelease{l})
|
||||||
|
sort.Sort(sort.Reverse(byRelease{l}))
|
||||||
|
|
||||||
|
fmt.Print(l)
|
||||||
|
}
|
26
interfaces/11-sort/product.go
Normal file
26
interfaces/11-sort/product.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type product struct {
|
||||||
|
title string
|
||||||
|
price money
|
||||||
|
released timestamp
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *product) String() string {
|
||||||
|
return fmt.Sprintf("%s: %s (%s)", p.title, p.price, p.released)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *product) discount(ratio float64) {
|
||||||
|
p.price *= money(1 - ratio)
|
||||||
|
}
|
41
interfaces/11-sort/timestamp.go
Normal file
41
interfaces/11-sort/timestamp.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type timestamp struct {
|
||||||
|
time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ts timestamp) String() string {
|
||||||
|
if ts.IsZero() {
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mon Jan 2 15:04:05 -0700 MST 2006
|
||||||
|
const layout = "2006/01"
|
||||||
|
return ts.Format(layout)
|
||||||
|
}
|
||||||
|
|
||||||
|
func toTimestamp(v interface{}) (ts timestamp) {
|
||||||
|
var t int
|
||||||
|
|
||||||
|
switch v := v.(type) {
|
||||||
|
case int:
|
||||||
|
t = v
|
||||||
|
case string:
|
||||||
|
t, _ = strconv.Atoi(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
ts.Time = time.Unix(int64(t), 0)
|
||||||
|
return ts
|
||||||
|
}
|
17
interfaces/12-marshaler/database.json
Normal file
17
interfaces/12-marshaler/database.json
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"title": "moby dick",
|
||||||
|
"price": 10,
|
||||||
|
"released": 118281600
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title": "odyssey",
|
||||||
|
"price": 15,
|
||||||
|
"released": 733622400
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title": "hobbit",
|
||||||
|
"price": 25,
|
||||||
|
"released": -62135596800
|
||||||
|
}
|
||||||
|
]
|
29
interfaces/12-marshaler/json.go
Normal file
29
interfaces/12-marshaler/json.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
)
|
||||||
|
|
||||||
|
func encode() ([]byte, error) {
|
||||||
|
l := list{
|
||||||
|
{Title: "moby dick", Price: 10, Released: toTimestamp(118281600)},
|
||||||
|
{Title: "odyssey", Price: 15, Released: toTimestamp("733622400")},
|
||||||
|
{Title: "hobbit", Price: 25},
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.MarshalIndent(l, "", "\t")
|
||||||
|
}
|
||||||
|
|
||||||
|
func decode(data []byte) (l list, err error) {
|
||||||
|
if err := json.Unmarshal(data, &l); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return l, nil
|
||||||
|
}
|
49
interfaces/12-marshaler/list.go
Normal file
49
interfaces/12-marshaler/list.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type list []*product
|
||||||
|
|
||||||
|
func (l list) String() string {
|
||||||
|
if len(l) == 0 {
|
||||||
|
return "Sorry. We're waiting for delivery 🚚.\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Sort(l)
|
||||||
|
var str strings.Builder
|
||||||
|
for _, p := range l {
|
||||||
|
fmt.Fprintf(&str, "* %s\n", p)
|
||||||
|
}
|
||||||
|
return str.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l list) discount(ratio float64) {
|
||||||
|
for _, p := range l {
|
||||||
|
p.discount(ratio)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// by default `list` sorts by `Title`.
|
||||||
|
func (l list) Len() int { return len(l) }
|
||||||
|
func (l list) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
|
||||||
|
func (l list) Less(i, j int) bool { return l[i].Title < l[j].Title }
|
||||||
|
|
||||||
|
// byRelease sorts by product release dates.
|
||||||
|
type byRelease struct {
|
||||||
|
list
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bp byRelease) Less(i, j int) bool {
|
||||||
|
return bp.list[i].Released.Before(bp.list[j].Released.Time)
|
||||||
|
}
|
21
interfaces/12-marshaler/main.go
Normal file
21
interfaces/12-marshaler/main.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// removed error handling for brevity (normally you shouldn't do this).
|
||||||
|
out, _ := encode()
|
||||||
|
fmt.Println(string(out))
|
||||||
|
|
||||||
|
l, _ := decode(out)
|
||||||
|
fmt.Print(l)
|
||||||
|
}
|
26
interfaces/12-marshaler/product.go
Normal file
26
interfaces/12-marshaler/product.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type product struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
Price money `json:"price"`
|
||||||
|
Released timestamp `json:"released,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *product) String() string {
|
||||||
|
return fmt.Sprintf("%s: %s (%s)", p.Title, p.Price, p.Released)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *product) discount(ratio float64) {
|
||||||
|
p.Price *= money(1 - ratio)
|
||||||
|
}
|
57
interfaces/12-marshaler/timestamp.go
Normal file
57
interfaces/12-marshaler/timestamp.go
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type timestamp struct {
|
||||||
|
time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ts timestamp) String() string {
|
||||||
|
if ts.IsZero() {
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mon Jan 2 15:04:05 -0700 MST 2006
|
||||||
|
const layout = "2006/01"
|
||||||
|
return ts.Format(layout)
|
||||||
|
}
|
||||||
|
|
||||||
|
// timestamp knows how to decode itself from json.
|
||||||
|
// UnmarshalJSON is an implementation of the json.Unmarshaler interface.
|
||||||
|
// json.Unmarshal and json.Decode call this method.
|
||||||
|
func (ts *timestamp) UnmarshalJSON(data []byte) error {
|
||||||
|
*ts = toTimestamp(string(data))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// timestamp knows how to encode itself to json.
|
||||||
|
// MarshalJSON is an implementation of the json.Marshaler interface.
|
||||||
|
// json.Marshal and json.Encode call this method.
|
||||||
|
func (ts timestamp) MarshalJSON() (out []byte, err error) {
|
||||||
|
// return ts.Time.MarshalJSON()
|
||||||
|
return strconv.AppendInt(out, ts.Unix(), 10), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func toTimestamp(v interface{}) (ts timestamp) {
|
||||||
|
var t int
|
||||||
|
|
||||||
|
switch v := v.(type) {
|
||||||
|
case int:
|
||||||
|
t = v
|
||||||
|
case string:
|
||||||
|
t, _ = strconv.Atoi(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
ts.Time = time.Unix(int64(t), 0)
|
||||||
|
return ts
|
||||||
|
}
|
17
interfaces/13-io/database.json
Normal file
17
interfaces/13-io/database.json
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"title": "moby dick",
|
||||||
|
"price": 10,
|
||||||
|
"released": 118281600
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title": "odyssey",
|
||||||
|
"price": 15,
|
||||||
|
"released": 733622400
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title": "hobbit",
|
||||||
|
"price": 25,
|
||||||
|
"released": -62135596800
|
||||||
|
}
|
||||||
|
]
|
59
interfaces/13-io/json.go
Normal file
59
interfaces/13-io/json.go
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func encode() ([]byte, error) {
|
||||||
|
l := list{
|
||||||
|
{Title: "moby dick", Price: 10, Released: toTimestamp(118281600)},
|
||||||
|
{Title: "odyssey", Price: 15, Released: toTimestamp("733622400")},
|
||||||
|
{Title: "hobbit", Price: 25},
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.MarshalIndent(l, "", "\t")
|
||||||
|
}
|
||||||
|
|
||||||
|
func decode(data []byte) (l list, err error) {
|
||||||
|
if err := json.Unmarshal(data, &l); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return l, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeFile(path string) (list, error) {
|
||||||
|
// ReadAll reads entire bytes from a file to memory.
|
||||||
|
data, err := ioutil.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return decode(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeFileObject(f *os.File) (list, error) {
|
||||||
|
data, err := ioutil.ReadAll(f)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return decode(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeReader(r io.Reader) (l list, err error) {
|
||||||
|
// data, err := ioutil.ReadAll(r)
|
||||||
|
// if err != nil {
|
||||||
|
// return nil, err
|
||||||
|
// }
|
||||||
|
// return decode(data)
|
||||||
|
|
||||||
|
return l, json.NewDecoder(r).Decode(&l)
|
||||||
|
}
|
49
interfaces/13-io/list.go
Normal file
49
interfaces/13-io/list.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type list []*product
|
||||||
|
|
||||||
|
func (l list) String() string {
|
||||||
|
if len(l) == 0 {
|
||||||
|
return "Sorry. We're waiting for delivery 🚚.\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Sort(l)
|
||||||
|
var str strings.Builder
|
||||||
|
for _, p := range l {
|
||||||
|
fmt.Fprintf(&str, "* %s\n", p)
|
||||||
|
}
|
||||||
|
return str.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l list) discount(ratio float64) {
|
||||||
|
for _, p := range l {
|
||||||
|
p.discount(ratio)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// by default `list` sorts by `Title`.
|
||||||
|
func (l list) Len() int { return len(l) }
|
||||||
|
func (l list) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
|
||||||
|
func (l list) Less(i, j int) bool { return l[i].Title < l[j].Title }
|
||||||
|
|
||||||
|
// byRelease sorts by product release dates.
|
||||||
|
type byRelease struct {
|
||||||
|
list
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bp byRelease) Less(i, j int) bool {
|
||||||
|
return bp.list[i].Released.Before(bp.list[j].Released.Time)
|
||||||
|
}
|
47
interfaces/13-io/main.go
Normal file
47
interfaces/13-io/main.go
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// removed error handling for brevity (normally you shouldn't do this).
|
||||||
|
|
||||||
|
// encode to memory (to a byte slice)
|
||||||
|
// out, _ := encode()
|
||||||
|
// fmt.Println(string(out))
|
||||||
|
|
||||||
|
// read from memory (from a byte slice):
|
||||||
|
// l, _ := decode(out)
|
||||||
|
|
||||||
|
// read from a file by name:
|
||||||
|
// l, _ := decodeFile("database.json")
|
||||||
|
|
||||||
|
// read from a file #1 (not good):
|
||||||
|
// f, _ := os.Open("database.json")
|
||||||
|
// l, _ := decodeFileObject(f)
|
||||||
|
// f.Close()
|
||||||
|
|
||||||
|
// read from a file #2 (better):
|
||||||
|
// f, _ := os.Open("database.json")
|
||||||
|
// l, _ := decodeReader(f)
|
||||||
|
// f.Close()
|
||||||
|
|
||||||
|
// read from memory (from a string):
|
||||||
|
// const data = `[
|
||||||
|
// { "title": "moby dick", "price": 10, "released": 118281600 },
|
||||||
|
// { "title": "odyssey", "price": 15, "released": 733622400 },
|
||||||
|
// { "title": "hobbit", "price": 25, "released": -62135596800 }
|
||||||
|
// ]`
|
||||||
|
// l, _ := decodeReader(strings.NewReader(data))
|
||||||
|
|
||||||
|
// read from inanc's web server:
|
||||||
|
// res, _ := http.Get("https://inancgumus.github.io/x/database.json")
|
||||||
|
// l, _ := decodeReader(res.Body)
|
||||||
|
// res.Body.Close()
|
||||||
|
|
||||||
|
// fmt.Print(l)
|
||||||
|
}
|
26
interfaces/13-io/product.go
Normal file
26
interfaces/13-io/product.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type product struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
Price money `json:"price"`
|
||||||
|
Released timestamp `json:"released,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *product) String() string {
|
||||||
|
return fmt.Sprintf("%s: %s (%s)", p.Title, p.Price, p.Released)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *product) discount(ratio float64) {
|
||||||
|
p.Price *= money(1 - ratio)
|
||||||
|
}
|
50
interfaces/13-io/timestamp.go
Normal file
50
interfaces/13-io/timestamp.go
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type timestamp struct {
|
||||||
|
time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ts timestamp) String() string {
|
||||||
|
if ts.IsZero() {
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mon Jan 2 15:04:05 -0700 MST 2006
|
||||||
|
const layout = "2006/01"
|
||||||
|
return ts.Format(layout)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ts *timestamp) UnmarshalJSON(data []byte) error {
|
||||||
|
*ts = toTimestamp(string(data))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ts timestamp) MarshalJSON() (out []byte, err error) {
|
||||||
|
return strconv.AppendInt(out, ts.Unix(), 10), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func toTimestamp(v interface{}) (ts timestamp) {
|
||||||
|
var t int
|
||||||
|
|
||||||
|
switch v := v.(type) {
|
||||||
|
case int:
|
||||||
|
t = v
|
||||||
|
case string:
|
||||||
|
t, _ = strconv.Atoi(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
ts.Time = time.Unix(int64(t), 0)
|
||||||
|
return ts
|
||||||
|
}
|
17
interfaces/14-composition/database.json
Normal file
17
interfaces/14-composition/database.json
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"title": "moby dick",
|
||||||
|
"price": 10,
|
||||||
|
"released": 118281600
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title": "odyssey",
|
||||||
|
"price": 15,
|
||||||
|
"released": 733622400
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title": "hobbit",
|
||||||
|
"price": 25,
|
||||||
|
"released": -62135596800
|
||||||
|
}
|
||||||
|
]
|
BIN
interfaces/14-composition/database.json.gz
Normal file
BIN
interfaces/14-composition/database.json.gz
Normal file
Binary file not shown.
59
interfaces/14-composition/json.go
Normal file
59
interfaces/14-composition/json.go
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func encode() ([]byte, error) {
|
||||||
|
l := list{
|
||||||
|
{Title: "moby dick", Price: 10, Released: toTimestamp(118281600)},
|
||||||
|
{Title: "odyssey", Price: 15, Released: toTimestamp("733622400")},
|
||||||
|
{Title: "hobbit", Price: 25},
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.MarshalIndent(l, "", "\t")
|
||||||
|
}
|
||||||
|
|
||||||
|
func decode(data []byte) (l list, err error) {
|
||||||
|
if err := json.Unmarshal(data, &l); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return l, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeFile(path string) (list, error) {
|
||||||
|
// ReadAll reads entire bytes from a file to memory.
|
||||||
|
data, err := ioutil.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return decode(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeFileObject(f *os.File) (list, error) {
|
||||||
|
data, err := ioutil.ReadAll(f)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return decode(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeReader(r io.Reader) (l list, err error) {
|
||||||
|
// data, err := ioutil.ReadAll(r)
|
||||||
|
// if err != nil {
|
||||||
|
// return nil, err
|
||||||
|
// }
|
||||||
|
// return decode(data)
|
||||||
|
|
||||||
|
return l, json.NewDecoder(r).Decode(&l)
|
||||||
|
}
|
49
interfaces/14-composition/list.go
Normal file
49
interfaces/14-composition/list.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type list []*product
|
||||||
|
|
||||||
|
func (l list) String() string {
|
||||||
|
if len(l) == 0 {
|
||||||
|
return "Sorry. We're waiting for delivery 🚚.\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Sort(l)
|
||||||
|
var str strings.Builder
|
||||||
|
for _, p := range l {
|
||||||
|
fmt.Fprintf(&str, "* %s\n", p)
|
||||||
|
}
|
||||||
|
return str.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l list) discount(ratio float64) {
|
||||||
|
for _, p := range l {
|
||||||
|
p.discount(ratio)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// by default `list` sorts by `Title`.
|
||||||
|
func (l list) Len() int { return len(l) }
|
||||||
|
func (l list) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
|
||||||
|
func (l list) Less(i, j int) bool { return l[i].Title < l[j].Title }
|
||||||
|
|
||||||
|
// byRelease sorts by product release dates.
|
||||||
|
type byRelease struct {
|
||||||
|
list
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bp byRelease) Less(i, j int) bool {
|
||||||
|
return bp.list[i].Released.Before(bp.list[j].Released.Time)
|
||||||
|
}
|
82
interfaces/14-composition/main.go
Normal file
82
interfaces/14-composition/main.go
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
// compress in bash:
|
||||||
|
// gzip -k database.json
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// removed error handling and x.Close() calls for brevity (normally you shouldn't do this).
|
||||||
|
// normally: fr.Close(), gr.Close() should also be called to release the resources.
|
||||||
|
|
||||||
|
// data, _ := ioutil.ReadFile("database.json")
|
||||||
|
// l, _ := decode(data)
|
||||||
|
|
||||||
|
// fmt.Print(string(data))
|
||||||
|
// fmt.Print(l)
|
||||||
|
|
||||||
|
// ---
|
||||||
|
|
||||||
|
// fr, _ := os.Open("database.json")
|
||||||
|
// tr := TeeReader(fr, os.Stdout)
|
||||||
|
// l, _ := decodeReader(tr)
|
||||||
|
// fmt.Print(l)
|
||||||
|
|
||||||
|
// ---
|
||||||
|
|
||||||
|
// fr, _ := os.Open("database.json.gz")
|
||||||
|
// gr, _ := gzip.NewReader(fr)
|
||||||
|
// tr := TeeReader(gr, os.Stdout)
|
||||||
|
// l, _ := decodeReader(tr)
|
||||||
|
// fmt.Print(l)
|
||||||
|
|
||||||
|
// ---
|
||||||
|
|
||||||
|
// TODO: FIX
|
||||||
|
// l, err := decodeGzip("database.json.gz", os.Stdout)
|
||||||
|
// l, err := decodeGzip("database.json.gz", ioutil.Discard)
|
||||||
|
// if err != nil {
|
||||||
|
// log.Fatalln(err)
|
||||||
|
// }
|
||||||
|
// fmt.Print("Products:\n", l)
|
||||||
|
}
|
||||||
|
|
||||||
|
// func decodeGzipFile(path string) (list, error) {
|
||||||
|
// fr, err := os.Open("database.json.gz")
|
||||||
|
// if err != nil {
|
||||||
|
// return nil, err
|
||||||
|
// }
|
||||||
|
// defer fr.Close()
|
||||||
|
|
||||||
|
// return decodeGzip(fr)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func decodeGzip(r io.Reader, w io.Writer) (list, error) {
|
||||||
|
// fr, err := os.Open("database.json.gz")
|
||||||
|
// if err != nil {
|
||||||
|
// return nil, err
|
||||||
|
// }
|
||||||
|
|
||||||
|
// gr, err := gzip.NewReader(fr)
|
||||||
|
// if err != nil {
|
||||||
|
// fr.Close()
|
||||||
|
// return nil, err
|
||||||
|
// }
|
||||||
|
|
||||||
|
// tr := TeeReader(gr, w)
|
||||||
|
// l, err := decodeReader(tr)
|
||||||
|
// if err != nil {
|
||||||
|
// fr.Close()
|
||||||
|
// gr.Close()
|
||||||
|
// return nil, err
|
||||||
|
// }
|
||||||
|
|
||||||
|
// fr.Close()
|
||||||
|
// gr.Close()
|
||||||
|
// return l, nil
|
||||||
|
// }
|
26
interfaces/14-composition/product.go
Normal file
26
interfaces/14-composition/product.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type product struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
Price money `json:"price"`
|
||||||
|
Released timestamp `json:"released,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *product) String() string {
|
||||||
|
return fmt.Sprintf("%s: %s (%s)", p.Title, p.Price, p.Released)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *product) discount(ratio float64) {
|
||||||
|
p.Price *= money(1 - ratio)
|
||||||
|
}
|
34
interfaces/14-composition/tee.go
Normal file
34
interfaces/14-composition/tee.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "io"
|
||||||
|
|
||||||
|
// TeeReader returns a Reader that writes to w what it reads from r.
|
||||||
|
// All reads from r performed through it are matched with
|
||||||
|
// corresponding writes to w. There is no internal buffering -
|
||||||
|
// the write must complete before the read completes.
|
||||||
|
// Any error encountered while writing is reported as a read error.
|
||||||
|
func TeeReader(r io.Reader, w io.Writer) io.Reader {
|
||||||
|
return &teeReader{r, w}
|
||||||
|
}
|
||||||
|
|
||||||
|
type teeReader struct {
|
||||||
|
r io.Reader
|
||||||
|
w io.Writer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *teeReader) Read(p []byte) (n int, err error) {
|
||||||
|
n, err = t.r.Read(p)
|
||||||
|
if n > 0 {
|
||||||
|
if n, err := t.w.Write(p[:n]); err != nil {
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
50
interfaces/14-composition/timestamp.go
Normal file
50
interfaces/14-composition/timestamp.go
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type timestamp struct {
|
||||||
|
time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ts timestamp) String() string {
|
||||||
|
if ts.IsZero() {
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mon Jan 2 15:04:05 -0700 MST 2006
|
||||||
|
const layout = "2006/01"
|
||||||
|
return ts.Format(layout)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ts *timestamp) UnmarshalJSON(data []byte) error {
|
||||||
|
*ts = toTimestamp(string(data))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ts timestamp) MarshalJSON() (out []byte, err error) {
|
||||||
|
return strconv.AppendInt(out, ts.Unix(), 10), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func toTimestamp(v interface{}) (ts timestamp) {
|
||||||
|
var t int
|
||||||
|
|
||||||
|
switch v := v.(type) {
|
||||||
|
case int:
|
||||||
|
t = v
|
||||||
|
case string:
|
||||||
|
t, _ = strconv.Atoi(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
ts.Time = time.Unix(int64(t), 0)
|
||||||
|
return ts
|
||||||
|
}
|
46
interfaces/_old/09-stringer/_handlemethods.go
Normal file
46
interfaces/_old/09-stringer/_handlemethods.go
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
// In the depths of the Go standard library's fmt package...
|
||||||
|
// Printing functions use the handleMethods method.
|
||||||
|
|
||||||
|
// Example:
|
||||||
|
// var pocket money = 10
|
||||||
|
// fmt.Println(pocket)
|
||||||
|
|
||||||
|
// the argument can be any type of value
|
||||||
|
// stores the pocket variable in the argument variable
|
||||||
|
// ^
|
||||||
|
// |
|
||||||
|
func (p *pp) handleMethods(argument interface{}) (handled bool) {
|
||||||
|
// ...
|
||||||
|
|
||||||
|
// Checks whether a given argument is an error or an fmt.Stringer
|
||||||
|
switch v := argument.(type) {
|
||||||
|
// ...
|
||||||
|
// If the argument is a Stringer, calls its String() method
|
||||||
|
case Stringer:
|
||||||
|
// ...
|
||||||
|
// pocket.String()
|
||||||
|
// ^
|
||||||
|
// |
|
||||||
|
p.fmtString(v.String(), verb)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
The original `handleMethods` code is more involved:
|
||||||
|
|
||||||
|
https://github.com/golang/go/blob/6f51082da77a1d4cafd5b7af0db69293943f4066/src/fmt/print.go#L574
|
||||||
|
|
||||||
|
-> 574#handleMethods(..)
|
||||||
|
-> 627#Stringer type check: If `v` is a Stringer, run:
|
||||||
|
-> 630#v.String()
|
||||||
|
*/
|
@ -7,6 +7,10 @@
|
|||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
type puzzle struct {
|
import "fmt"
|
||||||
product
|
|
||||||
|
type money float64
|
||||||
|
|
||||||
|
func (m money) String() string {
|
||||||
|
return fmt.Sprintf("$%.2f", m)
|
||||||
}
|
}
|
@ -7,6 +7,10 @@
|
|||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
type toy struct {
|
import "fmt"
|
||||||
product
|
|
||||||
|
type money float64
|
||||||
|
|
||||||
|
func (m money) String() string {
|
||||||
|
return fmt.Sprintf("$%.2f", m)
|
||||||
}
|
}
|
16
interfaces/_old/14-io-reader/money.go
Normal file
16
interfaces/_old/14-io-reader/money.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
// For more tutorials: https://blog.learngoprogramming.com
|
||||||
|
//
|
||||||
|
// Copyright © 2018 Inanc Gumus
|
||||||
|
// Learn Go Programming Course
|
||||||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
|
//
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type money float64
|
||||||
|
|
||||||
|
func (m money) String() string {
|
||||||
|
return fmt.Sprintf("$%.2f", m)
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user