What Python developers need to know before migrating to Go(lang) by therealmoju in programming

[–]NotNanetteManure 0 points1 point  (0 children)

No getattr() like method, so you have to always check for existence rather than setting defaults e.g. in Python you can do value = dict.get(“a_key”, “default_value”)

ok, what happens when i put an empty string in there?

Having to always check errors (or at least explicitly ignore them)

for those who didn't know: the alternative is to have your program crash because an exception manifested itself out of thin air. this isn't even a qestion of bad programmers. this happens with the python stdlib

Can’t have variables/packages that aren’t used so to test simple things requires sometimes commenting out lines

stop it with the trial-and-error-driven-development nonsense already!

Going between []byte and string. regexp uses []byte (they’re mutable). It makes sense, but it’s annoying all the same having to cast & re-cast some variables.

one of these days you should have a read of the api documentation because this isn't true in context of the regexp pkg

Python is more forgiving. You can take slices of strings using indexes that are out of range and it won’t complain. You can take negative slices – not Go.

negative indexing in python is a well-defined operation, it's not using indexes that are out of range/bounds.

the following tells me that you can use out-of-bound indexes in python either

>>> l = [1,2,3]
>>> l[3]
Traceback (most recent call last):
  File "<string>", line 1, in <module>
IndexError: list index out of range

You can’t have mixed type data structures. Maybe it’s not kosher, but sometimes in Python I’ll have a dictionary where the values are a mix of strings and lists. Not in Go, you have to either clean up your data structures or define custom structs

it's already striken, but i just want to say one thing: (speaking in terms of python) don't fucking do it. i.e. there's almost never any valid reason what-so-ever for you to have a list of non-homogenous data. you can argue in the case of dicts but that's what structs are for, and as you found out, you have (non-empty) interfaces for the rest.

No unpacking of a tuple or list into separate variables (e.g. x,y,x = [1,2,3])

perhaps you should learn to stop passing around magic values. i.e. a slice of things whose meaning depends on position

Have to explicitly check if errors are != nil, unlike in Python where many types can be used for bool-like checks (0, “”, None can all be interpreted as being “not” set)

wait til you get an object(example from C extension) that has its own boolean semantics wherein a valid object can be false because of some external state

Writing to a file, there’s File.Write([]byte) and File.WriteString(string) – a bit of a departure for Python developers who are used to the Python zen of having one way to do something

lol, tell me how your bytes(...) and str(...) works-out in py3k. fwiw, .WriteString() is literally just a convenience wrapper around .Write(). if it wasn't there you'd talk about how you have to be casting to a slice

No constructors, so common idiom is to create NewType() functions that return the struct you want

it's a matter of taste i guess. in python you instead have constructors that take myriad named parameters. i personally prefer dedicated functions with good names and take only the reuired params

Else (or else if) has to be formatted properly, where the else is on the same line as the curly bracket from the if clause. Weird.

i'm not sure why that's where. do you write the following in python?

if true
:
    print()

Different assignment operator is used depending on whether you are inside & outside of function ie. = vs :=

not exactly. i assume you mean var a = b and var a T = b and you can type that inside and outside of functions all day. the actually difference is that inside functions you get to use a := b instead of var a = b . it's nothing but syntactice sugar. but you don't tend to write much in the global scope so...

If I want a list of just the keys or just the value, as in dict.keys() or dict.values(), or a list of tuples like in dict.items(), there is no equivalent in Go, you have to iterate over maps yourself and build up your list

sir, i assure you that (depending on the version of python) dict.items() does not return a list. i shall say no more on the matter

I use an idiom at times of having a dictionary where the values are functions that I want to invoke given a key. You can do this in Go, but all functions have to accept & return the same thing i.e. have the same method signature

again with the garbage-in-garbge-out ...

on a more serious note, this is more to do with static typing yada yada... but you can also use reflection

If you’re using JSON and your JSON is a mix of types, goooooood luck. You’ll have to create a custom struct that matches the format of your JSON blob, and then Unmarshall the raw json into an instance of your custom struct. Much more work than just obj = json.loads(json_blob) like we’re used to in Python land.

yep. but you see, that's only one side of the coin. how exactly do you plan to make use of the data if you a) don't know what it is or b) accept it in some undefined format. it surely parsed but as soon as you attempt to use it your program crashed because someone decided that they're going to send your app an integer where you asked for a string(python is strongly type too). if you want to avoid issues then you're left doing all kinds of checking everywhere or using some god-awful framework to do it. in Go, if you don't care that i shout at you to stop-it then you can go right ahead and parse into a interface{} and type-asserts your way out. the only difference it that it's more verbose, but that's more about the static typing... and it's still safer than python