I understand Rust being type safe, but Im seeing syntax that Ive never seen in my life in Go which looks too messy

var test int < bruh what?

:=

func(u User) hi () { … } Where is the return type and why calling this fct doesnt require passing the u parameter but rather u.hi().

map := map[string] int {} < wtf

  • thingsiplay@beehaw.org
    link
    fedilink
    arrow-up
    6
    arrow-down
    2
    ·
    6 months ago

    The := operator is called walrus operator and is inspired by Python I think. It’s declaring a variable and assigning it in one go. map[string] int is defining a map (associative array or also known as dictionary in Python), with string as key type and int as value type. And the following {} is the body of the associative array, meaning empty content.

    I only played a bit with Go and did basic tutorials. I might go back to it at some point, but Zig is much more appealing to me. Go is too simple in the language to me, but on the other side, this is exactly whats appealing.

    • Atz@techhub.social
      link
      fedilink
      arrow-up
      7
      ·
      6 months ago

      @thingsiplay @urska
      ‘:=’ goes back to Pascal and maybe further. It also appears in PL/SQL. When I was learning to code my teacher told me to pronounce it as “becomes” since “equals” was for comparison.

      Took me a while to get used to the C style ‘==’.

    • Vivendi
      link
      fedilink
      arrow-up
      3
      ·
      6 months ago

      It goes back to mathematics pseudo code which was then used to sketch the ALGOL symbols way back in the days and which then gave birth to everything you know of programming today

      That shit’s quite ancient

      • thingsiplay@beehaw.org
        link
        fedilink
        arrow-up
        1
        arrow-down
        1
        ·
        edit-2
        6 months ago

        But that mathematical pseudo code has nothing in common with the walrus operator := in Python and Go. They are just the same symbols, but with a totally different meaning and use case. Its an operator designed specifically for programming languages, because that is not applicable to mathematics at all. In mathematics you don’t have an assignment operator a = 69 that cannot be used as part of an expression. Therefore you don’t need a dedicated := that yields an expression that can be used as part of an expression and create a variable if its not already. (Edit: I’m so sick of my stupidness.)

    • BatmanAoD@programming.dev
      link
      fedilink
      arrow-up
      3
      ·
      6 months ago

      I was curious about the Python connection because multiple comments mentioned it, but I’ve worked on multiple Python projects over the past dozen-ish years and never seen that operator.

      Turns out it was introduced in 3.8, released in 2019, so it was much too late to inspire Go, and most of the projects I’ve worked on were written to target an earlier Python version. It also has a substantially different meaning than in Go.

      I don’t know if there’s an “official” rationale for the Go syntax, but := is a fairly common (but not ubiquitous) math notation meaning “define the thing on the left to be equal to the expression on the right”, i.e. to distinguish it from the other use of =, i.e. “the expression on the left must be equal to the expression on the right.” Go’s usage matches this mathematical meaning of introducing a new variable definition pretty well.

      • thingsiplay@beehaw.org
        link
        fedilink
        arrow-up
        1
        ·
        edit-2
        6 months ago

        Turns out it was introduced in 3.8, released in 2019, so it was much too late to inspire Go

        You are probably right about that. But don’t forget that the operator didn’t made it day one, there was lot of discussion before and probably testing it in the beta releases before. But given how old Golang at that point is, you are right about my take on inspiration. This operator wasn’t a new invention in Python.

        It also has a substantially different meaning than in Go.

        I don’t know if there’s an “official” rationale for the Go syntax, but := is a fairly common (but not ubiquitous) math notation meaning “define the thing on the left to be equal to the expression on the right”, i.e. to distinguish it from the other use of =, i.e. “the expression on the left must be equal to the expression on the right.”

        Does it though? In both cases, Go and Python, the operator will assign a variable a value and also use it as an expression. That is useful in cases like for loops or other cases where you want immediately use the variable content as an expression. This cannot be done with the regular assignment operator a = 69, which itself is not an expression. So in practical terms, its the same in usability for Go and Python. So its doing the same for both languages and has the same differences to the assignment operator. (Edit: Read the reply, I learned something myself. That’s why its important that you don’t blindly teach people like I did.)

        • BatmanAoD@programming.dev
          link
          fedilink
          arrow-up
          3
          ·
          6 months ago

          In both cases, Go and Python, the operator will assign a variable a value and also use it as an expression.

          That is absolutely not true. foo := <expr> is a statement in Go, full stop. Just try something trivial like assigning to the output of :=: https://go.dev/play/p/nPINGc7LO8B

          It’s true that if and for let you use := but don’t let you use var, but you still can’t use the result of the assignment directly. So for instance you need if foo := <expr>; foo { ... } rather than just if foo := <expr> { ... }.

          • thingsiplay@beehaw.org
            link
            fedilink
            arrow-up
            3
            ·
            6 months ago

            Ok I see, I stand corrected then. Its a misconception I had without actually going through all of this, so my bad (will edit my replies to mark them). At least in Python we can do this print(foo := (bar := 3)) but not on its own as foo := 3 .