Skip to content

dhamodaran-pandiyan/wtfgolang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

What the f*ck Golang! 😱

Exploring and understanding Golang through surprising snippets.

The idea and design for this collection were inspired by satwikkansal's awesome project wtfpython.

Golang, one of the most facinating language, carrying out programming for scalable servers and large software systems.

Here's a fun project attempting to explain what exactly is happening under the hood for some counter-intuitive snippets and lesser-known features in Golang.

While some of the examples you see below may not be WTFs in the truest sense, but they'll reveal some of the interesting parts of Golang that you might be unaware of. I find it a nice way to learn the internals of a programming language, and I believe that you'll find it interesting too!

If you're an experienced Golang programmer, you can take it as a challenge to get most of them right in the first attempt. You may have already experienced some of them before, and I might be able to revive sweet old memories of yours! 😅

So, here we go...

Usage

A nice way to get the most out of these examples, in my opinion, is to read them in sequential order, and for every example:

  • Carefully read the initial code for setting up the example. If you're an experienced Python programmer, you'll successfully anticipate what's going to happen next most of the time.
  • Read the output snippets and,
    • Check if the outputs are the same as you'd expect.
    • Make sure if you know the exact reason behind the output being the way it is.
      • If the answer is no (which is perfectly okay), take a deep breath, and read the explanation (and if you still don't understand, shout out! and create an issue here).
      • If yes, give a gentle pat on your back, and you may skip to the next example.

👀 Examples

Section: Strain your brain!


▶ Careful with the braces!


1.

package main

import "fmt"

func main()  
{ 
    fmt.Println("hello there!")
}

Output:

main.go:6: syntax error: unexpected semicolon or newline before {

💡 Explanation:

  • In most other languages that use braces you get to choose where you place them. But, Go is different.

  • You can thank automatic semicolon injection (without lookahead) for this behavior. Yes, Go does have semicolons :-)

  • Working program,

    package main
    
    import "fmt"
    
    func main() {  
        fmt.Println("works!")
    }


▶ Strings are immutable!


1.

package main

import "fmt"

func main() {
    x := "text"
    x[0] = 'T'

    fmt.Println(x)
}

Output:

./prog.go:7:2: cannot assign to x[0] (value of type byte)

Go build failed.

💡 Explanation:

  • The immutability of strings is not the same as immutability of variables.

  • Immutability of strings means that the characters in the string cannot be changed. This holds true for Go.

  • Variables in Go are always mutable. When a string variable is changed, the internal fields of the variable (pointer and length) are changed. The address of variable never changes.

  • In Go, string is its own data type. At its core, it’s still a sequence of bytes, but:

    • It’s a fixed length. It doesn’t just continue on until a zero appears.
    • It comes with extra information: its length.
    • “Characters” or runes may span multiple bytes.
    • It’s immutable.
  • See the article on internals of string in Go http://research.swtch.com/godata.

  • Working program,

    package main
    
    import "fmt"
    
    func main() {
        x := "text"
        xbytes := []byte(x)
        xbytes[0] = 'T'
    
        fmt.Println(string(xbytes)) //prints Text
    }

Contributing

A few ways in which you can contribute to wtfgolang,

  • Suggesting new examples
  • Minor corrections like pointing out outdated snippets, typos, formatting errors, etc.
  • Identifying gaps (things like inadequate explanation, redundant examples, etc.)
  • Any creative suggestions to make this project more fun and useful

Please see CONTRIBUTING.md for more details. Feel free to create a new issue to discuss things.

Acknowledgements

The idea and design for this collection were initially inspired by satwikkansal's awesome project wtfpython.

Some nice Links!

  • Link 1
  • Link 2

🎓 License

WTFPL 2.0

© Dhamodaran pandiyan

Surprise your friends as well!