Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Лучший язык — это… или Пайтон против Суслика @Moscow Python Meetup № 81

Лучший язык — это… или Пайтон против Суслика @Moscow Python Meetup № 81

Евгений Соколов (YADRO, тимлид команды разработки СХД Tatlin). @Moscow Python Meetup №81
Python и Golang в чем-то похожи — легкий синтаксис, много библиотек, простота прототипирования. Но в последние годы у Go появляется ряд преимуществ, которые сподвигли меня и моих коллег перейти на этот язык. Я расскажу, что выиграет разработчик и бизнес, выбрав Go. И какие проблемы вы получите взамен.

Видео: https://moscowpython.ru/meetup/81/python-or-go/

MoscowPython: http://moscowpython.ru
Курсы Learn Python: http://learn.python.ru
Moscow Python Podcast: http://podcast.python.ru
Заявки на доклады: https://bit.ly/mp-speaker

Moscow Python Meetup
PRO

March 23, 2023
Tweet

More Decks by Moscow Python Meetup

Other Decks in Programming

Transcript

  1. View Slide

  2. 2

    View Slide

  3. 3

    View Slide

  4. View Slide

  5. View Slide

  6. Go = Simplify(C) + GoMultiThreading + DuckTyping*Interfaces + Tooling
    6

    View Slide

  7. View Slide

  8. 0 10 20 30 40 50 60 70 80 90

    View Slide

  9. merged_dict = dictA | dictB for k, v := range mapB {
    mapA[k] = v
    }

    View Slide

  10. Go = Simplify(C) + GoMultiThreading + DuckTyping*Interfaces + Tooling
    10

    View Slide

  11. View Slide

  12. Go = Simplify(C) + GoMultiThreading + DuckTyping*Interfaces + Tooling
    13

    View Slide

  13. GoMultiThreading = (OsScheduler + OsTread) * (GoScheduler + GoThread)Async
    + Channels
    14

    View Slide

  14. View Slide

  15. View Slide

  16. package main
    import (
    "fmt"
    "time"
    )
    func MyFunc(i int) error {
    <-time.After(time.Millisecond * 100)
    fmt.Println("Hello from", i)
    return nil
    }
    func main() {
    for i := 0; i < 10; i++ {
    MyFunc(i)
    }
    return
    }

    View Slide

  17. $ go run | ts '%.Ss'
    06.845330s Hello from 0
    06.945677s Hello from 1
    07.045978s Hello from 2
    07.146555s Hello from 3
    07.246888s Hello from 4
    07.347327s Hello from 5
    07.447700s Hello from 6
    07.547948s Hello from 7
    07.648438s Hello from 8
    07.748820s Hello from 9
    package main
    import (
    "fmt"
    "time"
    )
    func MyFunc(i int) error {
    <-time.After(time.Millisecond * 100)
    fmt.Println("Hello from", i)
    return nil
    }
    func main() {
    for i := 0; i < 10; i++ {
    MyFunc(i)
    }
    return
    }

    View Slide

  18. package main
    import (
    "fmt"
    "time"
    )
    func MyFunc(i int) error {
    <-time.After(time.Millisecond * 100)
    fmt.Println("Hello from", i)
    return nil
    }
    func main() {
    for i := 0; i < 10; i++ {
    go MyFunc(i)
    }
    <-time.After(time.Millisecond * 1100)
    return
    }

    View Slide

  19. $ go run | ts '%.Ss'
    43.417916s Hello from 4
    43.418109s Hello from 9
    43.418171s Hello from 7
    43.418196s Hello from 6
    43.418235s Hello from 8
    43.418292s Hello from 5
    43.418349s Hello from 1
    43.418404s Hello from 2
    43.418489s Hello from 0
    43.418549s Hello from 3
    package main
    import (
    "fmt"
    "time"
    )
    func MyFunc(i int) error {
    <-time.After(time.Millisecond * 100)
    fmt.Println("Hello from", i)
    return nil
    }
    func main() {
    for i := 0; i < 10; i++ {
    go MyFunc(i)
    }
    <-time.After(time.Millisecond * 1100)
    return
    }

    View Slide

  20. GoMultiThreading = (OsScheduler + OsTread) * (GoScheduler + GoThread)Async
    + Channels
    21

    View Slide

  21. import queue
    q = queue.Queue(maxsize=10)
    for i in range(10):
    q.put(i)
    while not q.empty():
    print(q.get())
    q := make(chan int, 10)
    for i := 0; i < 10; i++ {
    q <- i
    }
    out:
    for {
    select {
    case i := <-q:
    fmt.Println(i)
    default:
    break out
    }
    }

    View Slide

  22. $ go run | ts '%.Ss'
    56.622596s Run MyFunc goroutine
    56.622697s Waiting for data...
    56.622747s hehey
    package main
    import (
    "fmt"
    "time"
    )
    func MyFunc(data chan string) {
    fmt.Println("Waiting for data...")
    text := <-data
    fmt.Println(text)
    }
    func main() {
    dataChan := make(chan string)
    fmt.Println("Run MyFunc goroutine")
    go MyFunc(dataChan)
    dataChan <- "hehey“
    <-time.After(time.Second)
    }

    View Slide

  23. package main
    import (
    "fmt"
    "time"
    )
    func MyFunc(data chan string) {
    fmt.Println("Waiting for data...")
    text := <-data
    fmt.Println(text)
    }
    func main() {
    dataChan := make(chan string)
    dataChan <- "hehey"
    fmt.Println("Run MyFunc goroutine")
    go MyFunc(dataChan)
    <-time.After(time.Second)
    }

    View Slide

  24. $ go run | ts '%.Ss'
    fatal error: all goroutines are asleep - deadlock!
    goroutine 1 [chan send]:
    main.main()
    main.go:17 +0x59
    exit status 2
    package main
    import (
    "fmt"
    "time"
    )
    func MyFunc(data chan string) {
    fmt.Println("Waiting for data...")
    text := <-data
    fmt.Println(text)
    }
    func main() {
    dataChan := make(chan string)
    dataChan <- "hehey"
    fmt.Println("Run MyFunc goroutine")
    go MyFunc(dataChan)
    <-time.After(time.Second)
    }

    View Slide

  25. $ go run | ts '%.Ss'
    49.791121s Run MyFunc goroutine
    49.791244s Waiting for data...
    49.791295s hehey
    package main
    import (
    "fmt"
    "time"
    )
    func MyFunc(data chan string) {
    fmt.Println("Waiting for data...")
    text := <-data
    fmt.Println(text)
    }
    func main() {
    dataChan := make(chan string, 1)
    dataChan <- "hehey"
    fmt.Println("Run MyFunc goroutine")
    go MyFunc(dataChan)
    <-time.After(time.Second)
    }

    View Slide

  26. Go = Simplify(C) + GoMultiThreading + DuckTyping*Interfaces + Tooling
    27

    View Slide

  27. func main() {
    quackers := []Quacker{Duck{}, Duckling{}, Drake{}}
    for _, q := range quackers {
    q.Quack()
    }
    }
    package main
    import "fmt"
    type (
    Quacker interface {
    Quack()
    }
    Duck struct{}
    Drake struct{}
    Duckling struct{}
    )
    func (Duck) Quack() {
    fmt.Println("Duck: quack")
    }
    func (Drake) Quack() {
    fmt.Println("Drake: ...")
    }
    func (Duckling) Quack() {
    fmt.Println("Duckling: quack")
    }
    $ go run
    Duck: quack
    Duckling: quack
    Drake: ...

    View Slide

  28. Go = Simplify(C) + GoMultiThreading + DuckTyping*Interfaces + Tooling
    29

    View Slide

  29. Автоформатирование кода

    View Slide







  30. View Slide

  31. package main
    import (
    "github.com/stretchr/testify/assert"
    "testing"
    )
    func TestNewModule(t *testing.T) {
    assert.Equal(t, 5, 4)
    }

    View Slide

  32. $ go test
    main.go:3:8: no required module provides package
    github.com/stretchr/testify/assert; to add it:
    go get github.com/stretchr/testify/assert
    package main
    import (
    "github.com/stretchr/testify/assert"
    "testing"
    )
    func TestNewModule(t *testing.T) {
    assert.Equal(t, 5, 4)
    }

    View Slide

  33. $ go test
    main.go:3:8: no required module provides package
    github.com/stretchr/testify/assert; to add it:
    go get github.com/stretchr/testify/assert
    $ go get github.com/stretchr/testify/assert
    go: downloading github.com/stretchr/testify v1.8.1
    go: downloading github.com/davecgh/go-spew v1.1.1
    go: downloading github.com/pmezard/go-difflib v1.0.0
    go: downloading gopkg.in/yaml.v3 v3.0.1
    go: added github.com/davecgh/go-spew v1.1.1
    go: added github.com/pmezard/go-difflib v1.0.0
    go: added github.com/stretchr/testify v1.8.1
    go: added gopkg.in/yaml.v3 v3.0.1
    package main
    import (
    "github.com/stretchr/testify/assert"
    "testing"
    )
    func TestNewModule(t *testing.T) {
    assert.Equal(t, 5, 4)
    }

    View Slide

  34. $ go test
    main.go:3:8: no required module provides package
    github.com/stretchr/testify/assert; to add it:
    go get github.com/stretchr/testify/assert
    $ go get github.com/stretchr/testify/assert
    go: downloading github.com/stretchr/testify v1.8.1
    go: downloading github.com/davecgh/go-spew v1.1.1
    go: downloading github.com/pmezard/go-difflib v1.0.0
    go: downloading gopkg.in/yaml.v3 v3.0.1
    go: added github.com/davecgh/go-spew v1.1.1
    go: added github.com/pmezard/go-difflib v1.0.0
    go: added github.com/stretchr/testify v1.8.1
    go: added gopkg.in/yaml.v3 v3.0.1
    $ go test
    --- FAIL: TestNewModule (0.00s)
    maintest.go:7:
    Error Trace:
    /home/esokolov/test1/maintest.go:7
    Error: Not equal:
    expected: 5
    actual : 4
    Test: TestNewModule
    FAIL
    exit status 1
    FAIL testmodule 0.012s
    package main
    import (
    "github.com/stretchr/testify/assert"
    "testing"
    )
    func TestNewModule(t *testing.T) {
    assert.Equal(t, 5, 4)
    }

    View Slide

  35. Go = Simplify(C) + GoMultiThreading + DuckTyping*Interfaces + Tooling
    36

    View Slide

  36. Go = Simplify(C) + GoMultiThreading + DuckTyping*Interfaces + Tooling +
    if err!=nil{return err}*each function call
    37

    View Slide

  37. result, err = doSomething()
    if err != nil { return err }
    result2, err = doSomethingElse()
    if err != nil { return err }
    38

    View Slide

  38. if err != nil { return err }

    View Slide

  39. 40

    View Slide

  40. 41
    41




    View Slide

  41. View Slide