Likes & Dislikes: From Python To Golang

Try to use Golang as my main scripting language. It’s super fast and very powerful for parallel programming. Before the transition, I have been actively using Python for more than 5 years.

Here are things I like and dislike.

Check it out. And share your experience or feedback with me.

My Common Python Coding Mistakes


Related Resources In GitHub from Denny:

cheatsheet-golang-A4

cheatsheet-python-A4

Github


Likes:

  • Async programming: defer
  • Goroutine & channels
  • Named return values
  • Length and capacity concept about array

Dislikes:

  • Go doesn’t support ternary one liners.

Instead of:

# Python
return nums[0] if len(nums) != 0 else 0

It has to be:

// Golang
if len(nums) != 0 {
   return nums[0]
} else {
   return 0
}

// Or
if ret := 0; len(nums) != 0 {
   return nums[0]
}
return ret

Lots of discussions about the trade-off of this design: link, link.

Personally I’m still in favor of the Python compact way.

  • Get the last element of an array.
In Python we can simply use nums[-1] or nums[~0].
But in golang, it has to be: nums[len(nums)-1].
  • Reverse a list
In Python, reverse an array is as easy as: nums[::-1].

But in golang, you need to wrap up your own tiny function.
As far as I know, no built-in support.

Don't understand why.

Related Reading:



Original URL: https://dennyzhang.com/mytest

Connect with Denny In Slack Or MailList

Leave a Reply

Your email address will not be published. Required fields are marked *