Skip to content

Instantly share code, notes, and snippets.

@delqn
Last active October 8, 2018 06:13
Show Gist options
  • Save delqn/ae0bbce6a3b869d5cd006f2751663adc to your computer and use it in GitHub Desktop.
Save delqn/ae0bbce6a3b869d5cd006f2751663adc to your computer and use it in GitHub Desktop.
Go for the Impatient Pythonistas

Go for the Impatient (Pythonistas)

From the perspective of a Pythonista

Lists

  • Python: x = ["hey", "there"]
  • Go: x := []string{"hey", "there"}

Sets

  • Python: x = set(["hey", "there"])
  • Go (no sets):
  x := map[string]bool{"hey": true, "there": true}
  x["how"] = true
  delete(x, "are");

Raising / Catching Exceptions

return fmt.Errorf("%s:%d:%d: %v", f.Name(), line, col, err)
...
log.Fatal(err)

Named Tuples / High Performance Data Types

Point = namedtuple('Point', ['x', 'y'], verbose=True)
point1 = Point(8, 9)
point2 = namedtuple('Point', ['x', 'y'])(1, 2)`
  • Go:
type Point struct {x int; y int}
   var point2 = Point{8, 9}
point2 := struct {x int; y int}{1, 2}

Partially Applied Functions

Dictionaries

  • Python: x = {'name': 'bob'}
  • Go:
   // Initializes a map with space for 15 items
   m := make(map[string]int32, 15)
   ...
   // Initializes a map with an entry relating the name "bob" to the number 5
   m := map[string]int{"bob": 5}

List / Dictionary Comprehensions

Lambdas

  • Python: print((lambda x: x*2)(5))
  • Go: fmt.Println(func(x int)int{return x*2}(5))

Map, Filter and Reduce

Sorting

  • Python: x = [8,1,9]; sorted(x, reverse=True); x.sort()
  • Go: z := []int{8,1,9}; sort.Ints(z) // modifies the list in-place

Closures

package main

import (
   "fmt"
)

func increase() func()int {
   x := 0
   return func() int{
   	x += 1
   	return x
   }
}
func main() {
   nextInt := increase()
   fmt.Println("Hello, playground", nextInt ())
   fmt.Println(nextInt ())
}

Decorators

Monkey Patching

Testing

Overwrite / Override / Overload

Single Dispatch Generic Functions

range vs range

Circlular Dependencies

Garbage Collections

Interpretation / Compilation

Binary Artifacts (.pyc files)

Linting

REPL (IPython)

Classes and instantiation / destructino

Modules (init.py)

JSON

Multiple Inheritance

Polymorphism

Dictionary Keys (tuples: yes, lists: no)

List Destructuring

  • Python: (a, (b, [c, d])) = (1, [2, iter((3, 4))])

Keyword Function Arguments

  • Python: some_function(**{'name': 'bob', 'grade': 5})
  • Go: variadic arguments and/or sturcts: func someFunction(student ...struct{name string; grade int}) {

Generalized Function Signatures (*args, **kwargs)

Default Function Argument Values

Generators

Weird Errors

  • cannot take the address of <const> - src spec

Other Facts

  • make() can only be used to initialize slices, maps, and channels, and that, unlike the new() function, make() does not return a pointer (src)
  • appending to a slice

Books

Blogs

Videos

Tutorials

google-site-verification: googlef8fa8f3c4694c088.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment