this post was submitted on 13 Jun 2024
3 points (100.0% liked)

Programming

16780 readers
119 users here now

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



founded 1 year ago
MODERATORS
 

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

top 4 comments
sorted by: hot top controversial new old
[–] danhab99@programming.dev 2 points 2 months ago

I entirely disagree, Go has some of the most straightforward and meaningful syntax I've ever used.

[–] sping@lemmy.sdf.org 1 points 2 months ago (1 children)

The more time I spend on Lemmy the more depressed I am about its potential.

Stupid, wrong-headed comments get solid upvotes if they also hint at some popular sentiment. I even see comments that are literally unreadable nonsense get solidly upvoted, either by bots or by people who just like the vibe they feel from scanning it and don't care that it's gobbledygook. Some content makes me wonder if half of Lemmy is just LLMs barfing back and forth at each other.

Then this post is heavily upvoted, even though it's nothing more than "the syntax isn't the same as the other language(s) I have seen, waaaaa!". Is it just people like to see Go criticized? Because there are actual real issues that could be discussed.

[–] urska@lemmy.ca -1 points 2 months ago

your meds pal, take them

[–] rushaction@programming.dev 1 points 2 months ago* (last edited 2 months ago)

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 😎)