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

76 points
*

I’m going to try to help explain this, but i’ll be honest it feels like you’re coming from a place of frustration. I’m sorry about that, take a break :)

(I’m not a language expert, but here goes)

var test int < bruh what? :=

These are the two forms of variable declaration and the second one is a declaration and initialization short hand. I most commonly use :=. For instance:

foo := 1 // it's an int!
var bar uint16 // variable will be assigned the zero value for unit16 which is unsurprisingly, 0.

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

This has no return type because it returns no values. It does not require passing u. It’s a method on the User type, specifically u User is a method receiver. You might think of this akin to self or this variable in other languages. By convention it is a singke character of the type’s name.

If that function returned a value it might look like:

func(u User) hi() string {
    return "hi!"
}

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

This is confusing because of how it’s written. But the intent is to have a map (aka dictionary or hashmap) with string keys and int values. In your example it’s initializd to have no entries, the {}. Let me rewrite this a different way:

ages := map[string]int{
    "Alice": 38,
    "Bob": 37,
}

Hope this helps. In all honesty, Go’s language is very simple and actually rather clear. There’s definitely some funny bits, but these aren’t it. Take a break, come back to it later. It’s hard to learn if you are frustrated.

I also recommend doing the Tour of Go here. My engineers who found Go intimidating found it very accessible and helped them get through the learning code (as there is with any language).

Good luck (I’m on mobile and didn’t check my syntax, hopefully my code works 😎)

permalink
report
reply
15 points
*

but Im seeing syntax that Ive never seen in my life

Which languages do you know? What is your background?

What is wrong with “var test int”? There is no need for a return type, if the function returns nothing. Thats the language design and I think it is easy to remember.

func(u User) hi ()

u is something like self in Python and hi() is a method of User.

Please explain why do you think something is too messy, also with which languages you have already worked.

permalink
report
reply
11 points
*

Yea this is just syntax, every language does it a little different, most popular languages seem to derive off of C in some capacity. Some do it more different than others, and some are unholy conglomerations of unrelated languages that somehow works. Instead of saying why is this different, just ask how does this work. It’s made my life a lot simpler.

var test int is just int test in another language.

func (u User) hi () { ... } is just class User { void hi() { ... } } in another language (you can guess which language I’m referencing I bet).

map := map[string]int {} is just Map<String, Integer> map = new HashMap<>() in another (yes it’s java).

Also RTFM, this is all explained, just different!

Edit: I also know this is a very reductive view of things and there are larger differences, I was mostly approaching this from a newer developers understanding of things and just “getting it to work”.

permalink
report
reply
1 point
Deleted by creator
permalink
report
parent
reply
10 points
*

map := map[string] int {}

Not sure where you got your examples, but the spacing is pretty wonky on some (which can’t possibly help with confusion) and this one in particular causes a compile-time error. (It’s kindof trying to declare a variable named “map”, but “map” is a reserved word in Go.)

var test int < bruh what?

This article gives the reasoning for the type-after-variable-name declaration syntax.

:=

Lots of languages have a colon-equals construction. Python for one. It’s not terribly consistent what it means between languages. But in Go it declares and assigns one or more variables in one statement and tells Go to figure out the types of the variables for you so you don’t have to explicitly tell it the types to use.

func(u User) hi () { … }

That function (“method”, really, though in Go it’s more idiomatic to call it a “receiver func”) has no return values, so no return type. (Similar to declaring a function/method " void in other languages.)

The first pair of parens says to make this “function” a “method” of the “User” type (which must be declared in the same package for such a function declaration to work.) The whole “when I call it like u.hi(), don’t make me pass u as a parameter as well as putting u before the period” thing also has precedent in plenty of other languages. Python, again, is a good example.

Oh, and the second set of parens are where the function’s (non-receiver) parameters go. Your example just doesn’t take any. A function like func (u User) say(msg string) { ... }, for instance, could be called with u.say("Hey."). func (u User) ask(question string) string { ... } has a return type of string. So you could do var ans string = u.ask("Wuzzup?") or ans := u.ask("Wuzzup?").

I can’t say I was ever too taken aback with Go’s syntax. Just out of curiosity, what languages do you have experience with?

permalink
report
reply
0 points

C, C++, Assembly, java, Rust, Haskell, Prolog

permalink
report
parent
reply
7 points

Wow it shows how much it boils down to préférence at the end of the day.

I’m a Go fanboy, I cannot cite an aspect of the language I don’t like, find clunky…

Wow. I love syntax, the tooling, the speed, the errors, the testing framework.

permalink
report
reply

Programming

!programming@programming.dev

Create post

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person’s post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you’re posting long videos try to add in some form of tldr for those who don’t want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



Community stats

  • 3.9K

    Monthly active users

  • 1.7K

    Posts

  • 29K

    Comments