Исходники и презентации
This commit is contained in:
20
lessons/interfaces/call_interface_method/main.go
Normal file
20
lessons/interfaces/call_interface_method/main.go
Normal file
@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Interface interface {
|
||||
process(int) bool
|
||||
}
|
||||
|
||||
type String string
|
||||
|
||||
func (s String) process(size int) bool {
|
||||
return len(s) > size
|
||||
}
|
||||
|
||||
func main() {
|
||||
var i String = String("inteface")
|
||||
fmt.Println(i.process(10))
|
||||
fmt.Println(Interface.process(i, 10))
|
||||
fmt.Println(interface{ process(int) bool }.process(i, 10))
|
||||
}
|
||||
9
lessons/interfaces/compare_interfaces/main.go
Normal file
9
lessons/interfaces/compare_interfaces/main.go
Normal file
@ -0,0 +1,9 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var lhs interface{} = []int{1, 2, 3}
|
||||
var rhs interface{} = []int{1, 2, 3}
|
||||
fmt.Println(lhs == rhs)
|
||||
}
|
||||
9
lessons/interfaces/consumer_interface/entity/client.go
Normal file
9
lessons/interfaces/consumer_interface/entity/client.go
Normal file
@ -0,0 +1,9 @@
|
||||
package entity
|
||||
|
||||
type Client struct {
|
||||
Id int
|
||||
Name string
|
||||
Surname string
|
||||
Age int
|
||||
Address string
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
package service
|
||||
|
||||
import "golang_course/lessons/interfaces/consumer_interface/entity"
|
||||
|
||||
type clientUpdater interface {
|
||||
UpdateClient(entity.Client) error
|
||||
}
|
||||
|
||||
type ClientUpdater struct {
|
||||
repository clientUpdater
|
||||
}
|
||||
|
||||
func NewClientUpdater(repository clientUpdater) ClientUpdater {
|
||||
return ClientUpdater{
|
||||
repository: repository,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ClientUpdater) UpdateClient(client entity.Client) error {
|
||||
return s.repository.UpdateClient(client)
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
package service
|
||||
|
||||
import "golang_course/lessons/interfaces/consumer_interface/entity"
|
||||
|
||||
type clientGetter interface {
|
||||
GetClient(int) (entity.Client, error)
|
||||
}
|
||||
|
||||
type MessageSender struct {
|
||||
repository clientGetter
|
||||
}
|
||||
|
||||
func NewMessageSender(repository clientGetter) MessageSender {
|
||||
return MessageSender{
|
||||
repository: repository,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *MessageSender) SendMessage(userId int, message string) error {
|
||||
_, err := s.repository.GetClient(userId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// send message to client
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
package mysql
|
||||
|
||||
import "golang_course/lessons/interfaces/consumer_interface/entity"
|
||||
|
||||
type MySQLStorage struct{}
|
||||
|
||||
func (s *MySQLStorage) GetAllClients() ([]entity.Client, error) {
|
||||
// already implemented
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *MySQLStorage) GetClientsByAge(age int) ([]entity.Client, error) {
|
||||
// already implemented
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *MySQLStorage) GetClient(id int) (entity.Client, error) {
|
||||
// already implemented
|
||||
return entity.Client{}, nil
|
||||
}
|
||||
|
||||
func (s *MySQLStorage) RemoveClient(id int) error {
|
||||
// already implemented
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MySQLStorage) UpdateClient(client entity.Client) error {
|
||||
// already implemented
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MySQLStorage) CreateClient(client entity.Client) error {
|
||||
// already implemented
|
||||
return nil
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
package redis
|
||||
|
||||
import "golang_course/lessons/interfaces/consumer_interface/entity"
|
||||
|
||||
type RedisStorage struct{}
|
||||
|
||||
func (s *RedisStorage) GetAllClients() ([]entity.Client, error) {
|
||||
// already implemented
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *RedisStorage) GetClientsByAge(age int) ([]entity.Client, error) {
|
||||
// already implemented
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *RedisStorage) GetClient(id int) (entity.Client, error) {
|
||||
// already implemented
|
||||
return entity.Client{}, nil
|
||||
}
|
||||
|
||||
func (s *RedisStorage) RemoveClient(id int) error {
|
||||
// already implemented
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *RedisStorage) UpdateClient(client entity.Client) error {
|
||||
// already implemented
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *RedisStorage) CreateClient(client entity.Client) error {
|
||||
// already implemented
|
||||
return nil
|
||||
}
|
||||
26
lessons/interfaces/different_interfaces/main.go
Normal file
26
lessons/interfaces/different_interfaces/main.go
Normal file
@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
type Stringer interface {
|
||||
String() string
|
||||
}
|
||||
|
||||
// Embeds all types whose underlying type is []byte
|
||||
type AnyByteSlice interface {
|
||||
~[]byte
|
||||
}
|
||||
|
||||
// Embeds type union
|
||||
type Unsigned interface {
|
||||
uint | uintptr | uint8 | uint16 | uint32 | uint64
|
||||
}
|
||||
|
||||
func main() {
|
||||
var stringer Stringer
|
||||
_ = stringer
|
||||
|
||||
var any AnyByteSlice
|
||||
_ = any
|
||||
|
||||
var unsigned Unsigned
|
||||
_ = unsigned
|
||||
}
|
||||
34
lessons/interfaces/duck_typing/main.go
Normal file
34
lessons/interfaces/duck_typing/main.go
Normal file
@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Need to show solution
|
||||
|
||||
type Duck interface {
|
||||
Talk() string
|
||||
Walk()
|
||||
Swim()
|
||||
}
|
||||
|
||||
type Dog struct{}
|
||||
|
||||
func (d Dog) Talk() string {
|
||||
return "AAAGGGRRR"
|
||||
}
|
||||
|
||||
func (d Dog) Walk() {
|
||||
fmt.Println("Walking...")
|
||||
}
|
||||
|
||||
func (d Dog) Swim() {
|
||||
fmt.Println("Swimming...")
|
||||
}
|
||||
|
||||
func CatchDuck(duck Duck) {
|
||||
fmt.Println("Catching...")
|
||||
}
|
||||
|
||||
func main() {
|
||||
dog := Dog{}
|
||||
CatchDuck(dog)
|
||||
}
|
||||
21
lessons/interfaces/duck_typing_problem/main.go
Normal file
21
lessons/interfaces/duck_typing_problem/main.go
Normal file
@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import "context"
|
||||
|
||||
type UserService interface {
|
||||
Save(ctx context.Context, name string) (int64, error)
|
||||
Get(ctx context.Context, id int64) (interface{}, error)
|
||||
}
|
||||
|
||||
type UserRepository interface {
|
||||
Save(ctx context.Context, name string) (int64, error)
|
||||
Get(ctx context.Context, id int64) (interface{}, error)
|
||||
}
|
||||
|
||||
type Provider struct{}
|
||||
|
||||
func NewProvider() Provider {
|
||||
return Provider{}
|
||||
}
|
||||
|
||||
func (p *Provider) ProvideUserRepo() UserService
|
||||
18
lessons/interfaces/empty_interface/main.go
Normal file
18
lessons/interfaces/empty_interface/main.go
Normal file
@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
type Nothing interface{}
|
||||
|
||||
func main() {
|
||||
var nothing Nothing
|
||||
var empty interface{}
|
||||
var any any
|
||||
|
||||
nothing = empty
|
||||
nothing = any
|
||||
|
||||
empty = nothing
|
||||
empty = any
|
||||
|
||||
any = nothing
|
||||
any = empty
|
||||
}
|
||||
26
lessons/interfaces/empty_interfaces_conversion/main.go
Normal file
26
lessons/interfaces/empty_interfaces_conversion/main.go
Normal file
@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Aboutable interface {
|
||||
About() string
|
||||
}
|
||||
|
||||
type Book struct {
|
||||
name string
|
||||
}
|
||||
|
||||
func (book *Book) About() string {
|
||||
return "Book: " + book.name
|
||||
}
|
||||
|
||||
func main() {
|
||||
var a Aboutable = &Book{"Go 101"}
|
||||
fmt.Println(a) // &{Go 101}
|
||||
|
||||
var i interface{} = &Book{"Rust 101"}
|
||||
fmt.Println(i) // &{Rust 101}
|
||||
|
||||
i = 100
|
||||
fmt.Println(i) // 100
|
||||
}
|
||||
16
lessons/interfaces/empty_interfaces_use_case/main.go
Normal file
16
lessons/interfaces/empty_interfaces_use_case/main.go
Normal file
@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
type Cient struct{}
|
||||
type Admin struct{}
|
||||
|
||||
type Storage struct{}
|
||||
|
||||
func (s *Storage) Get(id int) (interface{}, error) {
|
||||
// already implemented
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *Storage) Set(id int, user interface{}) error {
|
||||
// already implemented
|
||||
return nil
|
||||
}
|
||||
11
lessons/interfaces/generic_tree/main.go
Normal file
11
lessons/interfaces/generic_tree/main.go
Normal file
@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
type BSTItem interface {
|
||||
Less(BSTItem) bool
|
||||
}
|
||||
|
||||
type BSTNode struct {
|
||||
item BSTItem
|
||||
left *BSTNode
|
||||
right *BSTNode
|
||||
}
|
||||
20
lessons/interfaces/incorrect_methods/main.go
Normal file
20
lessons/interfaces/incorrect_methods/main.go
Normal file
@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
type Shape interface {
|
||||
Area() float64
|
||||
}
|
||||
|
||||
type Circle struct {
|
||||
radius float64
|
||||
}
|
||||
|
||||
func (c Circle) Area() int {
|
||||
return int(3.14 * c.radius * c.radius)
|
||||
}
|
||||
|
||||
func main() {
|
||||
circle := Circle{}
|
||||
var iface interface{} = circle
|
||||
shape := iface.(Shape)
|
||||
_ = shape
|
||||
}
|
||||
29
lessons/interfaces/interface_allocation/main.go
Normal file
29
lessons/interfaces/interface_allocation/main.go
Normal file
@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// go build -gcflags '-l -m'
|
||||
|
||||
func printValue(v interface{}) {
|
||||
fmt.Println(v)
|
||||
/*
|
||||
println(v)
|
||||
_, _ = v.(int)
|
||||
*/
|
||||
}
|
||||
|
||||
func main() {
|
||||
var num1 int = 10
|
||||
var str1 string = "Hello"
|
||||
|
||||
printValue(num1)
|
||||
printValue(str1)
|
||||
|
||||
var num2 int = 10
|
||||
var str2 string = "Hello"
|
||||
|
||||
var i interface{}
|
||||
i = num2
|
||||
i = str2
|
||||
_ = i
|
||||
}
|
||||
20
lessons/interfaces/interface_assignment/main.go
Normal file
20
lessons/interfaces/interface_assignment/main.go
Normal file
@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
type A interface {
|
||||
Do()
|
||||
}
|
||||
|
||||
type B interface {
|
||||
Do()
|
||||
}
|
||||
|
||||
func main() {
|
||||
var a A
|
||||
var b B
|
||||
|
||||
// Does not work with
|
||||
// different packages
|
||||
|
||||
a = b
|
||||
b = a
|
||||
}
|
||||
34
lessons/interfaces/interface_cast/main.go
Normal file
34
lessons/interfaces/interface_cast/main.go
Normal file
@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
type BaseStorageImpl struct{}
|
||||
|
||||
func (s BaseStorageImpl) Close() {}
|
||||
|
||||
type SyncStorageImpl struct{}
|
||||
|
||||
func (s SyncStorageImpl) Close() {}
|
||||
func (s SyncStorageImpl) Sync() {}
|
||||
|
||||
type BaseStorage interface {
|
||||
Close()
|
||||
}
|
||||
|
||||
type SyncStorage interface {
|
||||
Close()
|
||||
Sync()
|
||||
}
|
||||
|
||||
func main() {
|
||||
var baseStorage BaseStorage = BaseStorageImpl{}
|
||||
var syncStorage SyncStorage = SyncStorageImpl{}
|
||||
|
||||
println("baseStorage:", baseStorage)
|
||||
baseStorage = syncStorage
|
||||
println("baseStorage:", baseStorage)
|
||||
|
||||
println("syncStorage:", syncStorage)
|
||||
syncStorageCasted := syncStorage.(interface{ Close() })
|
||||
println("syncStorageCasted:", syncStorageCasted)
|
||||
|
||||
//syncStorage = baseStorage
|
||||
}
|
||||
17
lessons/interfaces/interface_composition/main.go
Normal file
17
lessons/interfaces/interface_composition/main.go
Normal file
@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Greater interface {
|
||||
Hello()
|
||||
}
|
||||
|
||||
type Stranger interface {
|
||||
Greater
|
||||
Hello()
|
||||
}
|
||||
|
||||
func main() {
|
||||
var s Stranger
|
||||
fmt.Println(s)
|
||||
}
|
||||
15
lessons/interfaces/interface_composition_with_struct/main.go
Normal file
15
lessons/interfaces/interface_composition_with_struct/main.go
Normal file
@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
type Data struct{}
|
||||
|
||||
func (d Data) Print()
|
||||
|
||||
type StrData interface {
|
||||
Data
|
||||
String()
|
||||
}
|
||||
|
||||
func main() {
|
||||
var data StrData
|
||||
_ = data
|
||||
}
|
||||
25
lessons/interfaces/interface_copy/main.go
Normal file
25
lessons/interfaces/interface_copy/main.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type eface struct {
|
||||
typ unsafe.Pointer
|
||||
val unsafe.Pointer
|
||||
}
|
||||
|
||||
func main() {
|
||||
var value int = 100
|
||||
var i interface{} = value
|
||||
fmt.Println(i)
|
||||
|
||||
value = 200
|
||||
fmt.Println(i)
|
||||
|
||||
obj := (*eface)(unsafe.Pointer(&i))
|
||||
println("&value:", &value)
|
||||
println("obj.val:", obj.val)
|
||||
|
||||
}
|
||||
18
lessons/interfaces/interface_guard/main.go
Normal file
18
lessons/interfaces/interface_guard/main.go
Normal file
@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import "io"
|
||||
|
||||
type T struct {
|
||||
//...
|
||||
}
|
||||
|
||||
// Interface guard
|
||||
var _ io.ReadWriter = (*T)(nil)
|
||||
|
||||
func (t *T) Read(p []byte) (n int, err error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (t *T) Write(p []byte) (n int, err error) {
|
||||
return 0, nil
|
||||
}
|
||||
51
lessons/interfaces/interface_implementation/main.go
Normal file
51
lessons/interfaces/interface_implementation/main.go
Normal file
@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Square struct{}
|
||||
|
||||
func (s *Square) Area() float64 {
|
||||
return 0.0
|
||||
}
|
||||
|
||||
func (s *Square) Perimeter() float64 {
|
||||
return 0.0
|
||||
}
|
||||
|
||||
const (
|
||||
AreaMethod = iota
|
||||
PerimeterMethod
|
||||
)
|
||||
|
||||
type Interface struct {
|
||||
methods [2]func() float64
|
||||
}
|
||||
|
||||
func NewInterface(square *Square) Interface {
|
||||
return Interface{
|
||||
methods: [2]func() float64{
|
||||
AreaMethod: square.Area,
|
||||
PerimeterMethod: square.Perimeter,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (i *Interface) Area() float64 {
|
||||
return i.methods[AreaMethod]()
|
||||
}
|
||||
|
||||
func (i *Interface) Perimeter() float64 {
|
||||
return i.methods[PerimeterMethod]()
|
||||
}
|
||||
|
||||
func main() {
|
||||
// static dispatch
|
||||
square := &Square{}
|
||||
fmt.Println(square.Area())
|
||||
fmt.Println(square.Perimeter())
|
||||
|
||||
// dynamic dispatch
|
||||
iface := NewInterface(square)
|
||||
fmt.Println(iface.Area())
|
||||
fmt.Println(iface.Perimeter())
|
||||
}
|
||||
21
lessons/interfaces/interface_internals/main.go
Normal file
21
lessons/interfaces/interface_internals/main.go
Normal file
@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type eface struct {
|
||||
typ unsafe.Pointer
|
||||
val unsafe.Pointer
|
||||
}
|
||||
|
||||
func main() {
|
||||
var value int = 100
|
||||
var i interface{} = value
|
||||
fmt.Println("before:", i)
|
||||
|
||||
obj := (*eface)(unsafe.Pointer(&i))
|
||||
*(*int)(obj.val) = 200
|
||||
fmt.Println("after:", i)
|
||||
}
|
||||
7
lessons/interfaces/interface_key_in_map/main.go
Normal file
7
lessons/interfaces/interface_key_in_map/main.go
Normal file
@ -0,0 +1,7 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
var data []int
|
||||
dictionary := make(map[interface{}]struct{})
|
||||
dictionary[data] = struct{}{}
|
||||
}
|
||||
15
lessons/interfaces/interface_not_nil_1/main.go
Normal file
15
lessons/interfaces/interface_not_nil_1/main.go
Normal file
@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type MyError struct{}
|
||||
|
||||
func (e *MyError) Error() string {
|
||||
return "error"
|
||||
}
|
||||
|
||||
func main() {
|
||||
var pointer *MyError = nil
|
||||
var err error = pointer
|
||||
fmt.Println("nil:", err == nil)
|
||||
}
|
||||
31
lessons/interfaces/interface_not_nil_2/main.go
Normal file
31
lessons/interfaces/interface_not_nil_2/main.go
Normal file
@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func ReadFile(filename string) (err error) {
|
||||
var internalErr *os.PathError
|
||||
|
||||
if filename == "" {
|
||||
return internalErr
|
||||
}
|
||||
|
||||
// reading...
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
err := ReadFile("")
|
||||
if err != nil {
|
||||
fmt.Println("error")
|
||||
} else {
|
||||
fmt.Println("nil")
|
||||
}
|
||||
|
||||
fmt.Println("value of err: ", err)
|
||||
fmt.Printf("type of err: %T\n", err)
|
||||
fmt.Println("(err == nil): ", err == nil)
|
||||
|
||||
}
|
||||
16
lessons/interfaces/interface_not_nil_3/main.go
Normal file
16
lessons/interfaces/interface_not_nil_3/main.go
Normal file
@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Data struct {
|
||||
value int
|
||||
}
|
||||
|
||||
func Process(i interface{}) {
|
||||
fmt.Println("nil:", i == nil)
|
||||
}
|
||||
|
||||
func main() {
|
||||
var data *Data = nil
|
||||
Process(data)
|
||||
}
|
||||
58
lessons/interfaces/interface_performance/performance_test.go
Normal file
58
lessons/interfaces/interface_performance/performance_test.go
Normal file
@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
// go test -bench=. -benchmem performance_test.go
|
||||
|
||||
type ValueInterface interface {
|
||||
Add() int
|
||||
}
|
||||
|
||||
type Value struct {
|
||||
number int
|
||||
}
|
||||
|
||||
//go:noinline
|
||||
func (v Value) Add() int {
|
||||
return v.number + v.number
|
||||
}
|
||||
|
||||
type Value2 struct {
|
||||
number int
|
||||
}
|
||||
|
||||
//go:noinline
|
||||
func (v Value2) Add() int {
|
||||
return v.number + v.number
|
||||
}
|
||||
|
||||
var Result int
|
||||
|
||||
func Get(index int) ValueInterface {
|
||||
if index == 0 {
|
||||
return Value{}
|
||||
} else {
|
||||
return Value2{}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkDirect(b *testing.B) {
|
||||
var value Value
|
||||
for i := 0; i < b.N; i++ {
|
||||
Result = value.Add()
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkWithInterface1(b *testing.B) {
|
||||
var iface ValueInterface = Get(0)
|
||||
for i := 0; i < b.N; i++ {
|
||||
Result = iface.Add()
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkWithInterface2(b *testing.B) {
|
||||
var iface ValueInterface = Get(1)
|
||||
for i := 0; i < b.N; i++ {
|
||||
Result = iface.(Value2).Add()
|
||||
}
|
||||
}
|
||||
24
lessons/interfaces/interface_with_receiver/main.go
Normal file
24
lessons/interfaces/interface_with_receiver/main.go
Normal file
@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
type Interface interface {
|
||||
Do()
|
||||
}
|
||||
|
||||
type Object1 struct{}
|
||||
|
||||
func (o *Object1) Do() {}
|
||||
|
||||
type Object2 struct{}
|
||||
|
||||
func (o Object2) Do() {}
|
||||
|
||||
func main() {
|
||||
var i Interface
|
||||
|
||||
i = &Object1{}
|
||||
|
||||
i = Object2{}
|
||||
i = &Object2{}
|
||||
|
||||
_ = i
|
||||
}
|
||||
23
lessons/interfaces/interface_with_receiver_reason/main.go
Normal file
23
lessons/interfaces/interface_with_receiver_reason/main.go
Normal file
@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
type Getter interface {
|
||||
Get() interface{}
|
||||
}
|
||||
|
||||
type PointerGetter struct{}
|
||||
|
||||
func (p *PointerGetter) Get() interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
func get() PointerGetter {
|
||||
return PointerGetter{}
|
||||
}
|
||||
|
||||
func main() {
|
||||
var ptrGetter PointerGetter
|
||||
ptrGetter.Get() // ok
|
||||
|
||||
var getter Getter = get() // error
|
||||
_ = getter
|
||||
}
|
||||
11
lessons/interfaces/numbers_comparison/main.go
Normal file
11
lessons/interfaces/numbers_comparison/main.go
Normal file
@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var x interface{} = 3
|
||||
var y interface{} = 3
|
||||
fmt.Println(x == y)
|
||||
}
|
||||
31
lessons/interfaces/polymorphism/main.go
Normal file
31
lessons/interfaces/polymorphism/main.go
Normal file
@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Figure interface {
|
||||
Area() int
|
||||
}
|
||||
|
||||
type Square struct {
|
||||
x int
|
||||
}
|
||||
|
||||
func (s Square) Area() int {
|
||||
return s.x * s.x
|
||||
}
|
||||
|
||||
type Rectangle struct {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
func (r Rectangle) Area() int {
|
||||
return r.x * r.y
|
||||
}
|
||||
|
||||
func main() {
|
||||
figures := []Figure{Square{10}, Rectangle{10, 20}}
|
||||
for _, figure := range figures {
|
||||
fmt.Println(figure.Area())
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
package service
|
||||
|
||||
import "golang_course/lessons/interfaces/producer_interface/storage"
|
||||
|
||||
type ClientUpdater struct {
|
||||
repository storage.ClientStorage
|
||||
}
|
||||
|
||||
func NewClientUpdater(repository storage.ClientStorage) ClientUpdater {
|
||||
return ClientUpdater{
|
||||
repository: repository,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ClientUpdater) UpdateClient(client storage.Client) error {
|
||||
return s.repository.UpdateClient(client)
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
package service
|
||||
|
||||
import "golang_course/lessons/interfaces/producer_interface/storage"
|
||||
|
||||
type MessageSender struct {
|
||||
repository storage.ClientStorage
|
||||
}
|
||||
|
||||
func NewMessageSender(repository storage.ClientStorage) MessageSender {
|
||||
return MessageSender{
|
||||
repository: repository,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *MessageSender) SendMessage(userId int, message string) error {
|
||||
_, err := s.repository.GetClient(userId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// send message to client
|
||||
|
||||
return nil
|
||||
}
|
||||
19
lessons/interfaces/producer_interface/storage/client.go
Normal file
19
lessons/interfaces/producer_interface/storage/client.go
Normal file
@ -0,0 +1,19 @@
|
||||
package storage
|
||||
|
||||
type Client struct {
|
||||
Id int
|
||||
Name string
|
||||
Surname string
|
||||
Age int
|
||||
Address string
|
||||
}
|
||||
|
||||
type ClientStorage interface {
|
||||
GetAllClients() ([]Client, error)
|
||||
GetClientsByAge(int) ([]Client, error)
|
||||
GetClient(int) (Client, error)
|
||||
RemoveClient(int) error
|
||||
UpdateClient(Client) error
|
||||
CreateClient(Client) error
|
||||
// ...
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
package mysql
|
||||
|
||||
import "golang_course/lessons/interfaces/producer_interface/storage"
|
||||
|
||||
type MySQLStorage struct{}
|
||||
|
||||
func (s *MySQLStorage) GetAllClients() ([]storage.Client, error) {
|
||||
// already implemented
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *MySQLStorage) GetClientsByAge(age int) ([]storage.Client, error) {
|
||||
// already implemented
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *MySQLStorage) GetClient(id int) (storage.Client, error) {
|
||||
// already implemented
|
||||
return storage.Client{}, nil
|
||||
}
|
||||
|
||||
func (s *MySQLStorage) RemoveClient(id int) error {
|
||||
// already implemented
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MySQLStorage) UpdateClient(client storage.Client) error {
|
||||
// already implemented
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MySQLStorage) CreateClient(client storage.Client) error {
|
||||
// already implemented
|
||||
return nil
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
package redis
|
||||
|
||||
import "golang_course/lessons/interfaces/producer_interface/storage"
|
||||
|
||||
type RedisStorage struct{}
|
||||
|
||||
func (s *RedisStorage) GetAllClients() ([]storage.Client, error) {
|
||||
// already implemented
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *RedisStorage) GetClientsByAge(age int) ([]storage.Client, error) {
|
||||
// already implemented
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *RedisStorage) GetClient(id int) (storage.Client, error) {
|
||||
// already implemented
|
||||
return storage.Client{}, nil
|
||||
}
|
||||
|
||||
func (s *RedisStorage) RemoveClient(id int) error {
|
||||
// already implemented
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *RedisStorage) UpdateClient(client storage.Client) error {
|
||||
// already implemented
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *RedisStorage) CreateClient(client storage.Client) error {
|
||||
// already implemented
|
||||
return nil
|
||||
}
|
||||
17
lessons/interfaces/slice_values_to_slice_interfaces/main.go
Normal file
17
lessons/interfaces/slice_values_to_slice_interfaces/main.go
Normal file
@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var interfaces []interface{}
|
||||
values := []int{100, 200, 300, 400, 500}
|
||||
|
||||
// interfaces = values -> compilation error
|
||||
|
||||
interfaces = make([]interface{}, len(values))
|
||||
for idx := 0; idx < len(values); idx++ {
|
||||
interfaces[idx] = values[idx]
|
||||
}
|
||||
|
||||
fmt.Println(interfaces...)
|
||||
}
|
||||
15
lessons/interfaces/static_and_dynamic_type/main.go
Normal file
15
lessons/interfaces/static_and_dynamic_type/main.go
Normal file
@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Stringer interface {
|
||||
String() string
|
||||
}
|
||||
|
||||
func main() {
|
||||
var s Stringer // static type
|
||||
s = time.Time{} // dynamic type
|
||||
_ = s
|
||||
}
|
||||
23
lessons/interfaces/type_assertion_1/main.go
Normal file
23
lessons/interfaces/type_assertion_1/main.go
Normal file
@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var value int = 100
|
||||
var i interface{} = value
|
||||
|
||||
converted1, ok1 := i.(int)
|
||||
if ok1 {
|
||||
fmt.Println("converted1 int:", converted1)
|
||||
}
|
||||
|
||||
converted2, ok2 := i.(float32)
|
||||
if ok2 {
|
||||
fmt.Println("converted2 float32:", converted2)
|
||||
}
|
||||
|
||||
converted3 := i.(int)
|
||||
fmt.Println("converted3 int:", converted3)
|
||||
converted4 := i.(float32)
|
||||
fmt.Println("converted4 float32:", converted4)
|
||||
}
|
||||
21
lessons/interfaces/type_assertion_2/main.go
Normal file
21
lessons/interfaces/type_assertion_2/main.go
Normal file
@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type fooer interface{ foo() }
|
||||
type barer interface{ bar() }
|
||||
type foobarer interface {
|
||||
foo()
|
||||
bar()
|
||||
}
|
||||
|
||||
type thing struct{}
|
||||
|
||||
func (t *thing) foo() {}
|
||||
func (t *thing) bar() {}
|
||||
|
||||
func main() {
|
||||
var i foobarer = &thing{}
|
||||
_, ok := i.(fooer)
|
||||
fmt.Println("result:", ok)
|
||||
}
|
||||
18
lessons/interfaces/type_assertion_3/main.go
Normal file
18
lessons/interfaces/type_assertion_3/main.go
Normal file
@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type fooer interface{ foo() }
|
||||
|
||||
type thing struct{}
|
||||
|
||||
func (t *thing) foo() {}
|
||||
func (t *thing) bar() {}
|
||||
|
||||
func main() {
|
||||
var i fooer = &thing{}
|
||||
|
||||
// dynamically checking for certain methods
|
||||
_, ok := i.(interface{ bar() })
|
||||
fmt.Println("result:", ok)
|
||||
}
|
||||
20
lessons/interfaces/type_switch_1/main.go
Normal file
20
lessons/interfaces/type_switch_1/main.go
Normal file
@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func do(i interface{}) {
|
||||
switch v := i.(type) {
|
||||
case int:
|
||||
fmt.Println("int type:", v)
|
||||
case string, fmt.Stringer:
|
||||
fmt.Println("string type:", v)
|
||||
default:
|
||||
fmt.Println("unknown type:", v)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
do(21)
|
||||
do("hello")
|
||||
do(true)
|
||||
}
|
||||
30
lessons/interfaces/type_switch_2/main.go
Normal file
30
lessons/interfaces/type_switch_2/main.go
Normal file
@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type fooer interface{ foo() }
|
||||
type barer interface{ bar() }
|
||||
type foobarer interface {
|
||||
foo()
|
||||
bar()
|
||||
}
|
||||
|
||||
type thing struct{}
|
||||
|
||||
func (t *thing) foo() {}
|
||||
func (t *thing) bar() {}
|
||||
|
||||
var i foobarer = &thing{}
|
||||
|
||||
func main() {
|
||||
switch v := i.(type) {
|
||||
case fooer:
|
||||
fmt.Println("fooer:", v)
|
||||
case barer:
|
||||
fmt.Println("barer:", v)
|
||||
case foobarer:
|
||||
fmt.Println("foobarer:", v)
|
||||
default:
|
||||
panic("none of them")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user