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

March 23, 2023
Tweet

More Decks by Moscow Python Meetup

Other Decks in Programming

Transcript

  1. 2

  2. 3

  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++ { MyFunc(i) } return }
  4. $ 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 }
  5. 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 }
  6. $ 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 }
  7. 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 } }
  8. $ 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) }
  9. 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) }
  10. $ 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) }
  11. $ 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) }
  12. 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: ...
  13. $ 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) }
  14. $ 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) }
  15. $ 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) }
  16. Go = Simplify(C) + GoMultiThreading + DuckTyping*Interfaces + Tooling +

    if err!=nil{return err}*each function call 37
  17. result, err = doSomething() if err != nil { return

    err } result2, err = doSomethingElse() if err != nil { return err } 38
  18. 40