News for hackers. Stuff that matters.
Re:Learning a new language, or how I gained familiarity with Go by kubb on Wednesday August 17, 2022 @09:45AM (#32496289)
It's always important to ask yourself what concepts you knew before you learned a new technology X. For someone who knows C++ and Java there are no new concepts in Go, so the tour of Go should suffice.

For someone who doesn't know what a function, a pointer, and a string is, there's more work to be done.

One of the reasons Go is praised for simplicity is that it only requires us to know basic concepts. You already know how to call a function and store some integers in a struct. All you need is to get used to the new syntax.

For those who like to flex their brains and learn to think in new ways, this could be a disadvantage, but for someone overwhelmed with information it's a godsend.

On the other hand, if you always limit yourself to Go then it will be harder to pick up other technologies that take advantage of concepts not featured there.

Re:Learning a new language, or how I gained familiarity with Go by thejammahimself on Wednesday August 17, 2022 @09:58AM (#32496473)
I feel like a significant part of learning a new language is also learning the standard library alongside the new syntax. Especially because these can be quite different between different languages.
Re:Learning a new language, or how I gained familiarity with Go by Groxx on Wednesday August 17, 2022 @01:33PM (#32499329)
Yeah. Particularly after having learned 2 or 3 languages, learning the N+1th language is often the work of like an hour or three.

Learning the standard library, and the patterns it has encouraged in the community, takes years and is a moving target. Even a basic (accurate) "feel" for things generally takes weeks, regardless of how "simple" the language is. You might be able to produce code prior to that, but being able to predict where something should be, or how to find it, takes time.

Re:Learning a new language, or how I gained familiarity with Go by nerdponx on Wednesday August 17, 2022 @12:19PM (#32498313)
Not just the standard library, but also its idioms and the tools that people commonly use to work with the language (compiler, build tool, etc).
Re:Learning a new language, or how I gained familiarity with Go by mxuribe on Wednesday August 17, 2022 @11:16AM (#32497501)
> I feel like a significant part of learning a new language is also learning the standard library alongside the new syntax...

OMG, you are so right! I feel the same. Knowing what the standard libraries bring (or don't bring) for each language that one learsn is immensely helpful. Sometimes this helps me decide which language to use for particular use-case/project, or sometimes it helps me better understand what the level of effort will be for a project if i use langugae X vs language Y, and on and on, etc. I feel like a tour of each language's standard libraries is a must when learning a new language; or at least a cusrosry review of the most important and/.or most often used functions, etc. of said standard libs.

Re:Learning a new language, or how I gained familiarity with Go by kubb on Wednesday August 17, 2022 @10:07AM (#32496582)
What do you think about rosetta code, et al? For me they reduce the standard library issue quite a bit, combined with googling "how to do x in y".
Re:Learning a new language, or how I gained familiarity with Go by eweise on Wednesday August 17, 2022 @04:40PM (#32501417)
Trying to find something I agree with in this post. I programmed in Java for decades and found that there were plenty of new concepts to learn in Go. Not sure asking myself what concepts I knew would help me in learning Go, especially since I know the concepts, I don't need to ask myself whether I know them. Some concepts not in Java

- Pointers. These take a while to understand and use effectively. https://www.timr.co/go-interfaces-the-tricky-parts

- Error handling. Very different than exceptions

- Interfaces. Not quite the same as Java's and used differently in some cases

- Channels. Java doesn't have these

- Context. passing these around everywhere instead of just sticking on a threadlocal.

Go will flex your brain plenty since you have to unlearn what you thought were good programming practices such as only one return statement per method.

Re:Learning a new language, or how I gained familiarity with Go by eikenberry on Wednesday August 17, 2022 @04:23PM (#32501246)
> [..] there are no new concepts in Go [..]

Except for native support for CSP and that programs should be written in a CSP style. Learning Go and skipping CSP is not learning Go.

Re:Learning a new language, or how I gained familiarity with Go by morelisp on Wednesday August 17, 2022 @04:35PM (#32501368)
> Learning Go and skipping CSP is not learning Go.

Learning Go seems to be inevitably "learning CSP", using way too much "CSP", then eventually coming to your senses and going back to atomic types and locks for probably 70% of cases.

Re:Learning a new language, or how I gained familiarity with Go by eikenberry on Wednesday August 17, 2022 @05:29PM (#32501960)
Thinking CSP is something you can replace with atomic types and locks is a common misconception. Goroutines and channels are the primitives used by CSP, but just using them is not CSP. CSP is a whole program design system that can use all the concurrency primitives. There is no conflict between the using CSP and locks/atomic types. They work well together as they have different uses.

The misconception is pretty widespread and cancerous to the community. Ignoring CSP is ignoring Go's best feature for writing better code. It takes some time to learn to do well (I'm still working on it) as it is a different paradigm than most people are used to, but it is well worth the effort IMO.

Re:Learning a new language, or how I gained familiarity with Go by Xeoncross on Wednesday August 17, 2022 @11:26AM (#32497617)
While I agree there aren't really any new concepts, I can think of two areas in Go that C++ and Java don't really prepare you for: error handling / nesting and classes. Go doesn't use exceptions or classes for anything really. Instead it's all DI/object embedding + interfaces and nested error values + context which can be more of a mind hurdle than expected since inheritance and try/catch are so common these days.
Re:Learning a new language, or how I gained familiarity with Go by skrtskrt on Wednesday August 17, 2022 @01:28PM (#32499253)
Go channels are one that's pretty new depending on the language. I came from Python using async for all concurrency, so channels are an odd concept.

Passing context around is an interesting one too

Re:Learning a new language, or how I gained familiarity with Go by kubb on Wednesday August 17, 2022 @01:38PM (#32499402)
yes, channels and goroutines are a concept that doesn't transfer from C++ or Java :) good job pointing out the exception!
Re:Learning a new language, or how I gained familiarity with Go by fnord123 on Wednesday August 17, 2022 @03:57PM (#32500987)
> good job pointing out the exception!

No, go doesn't have exceptions. :D

To answer the sibling comment (I don't have enough karma to go deeper), Java NIO channels are barely usable. Using them is the quickest way to inject bugs into your code I've seen in Java.

Go channels are closer to blocking queues or a selection of concurrent queues depending on whether it's bounded or unbounded, or whether you use select, etc.

Re:Learning a new language, or how I gained familiarity with Go by kubb on Thursday August 18, 2022 @05:11AM (#32507097)
it has panic/recover though which is like exceptions :)
Re:Learning a new language, or how I gained familiarity with Go by 62951413 on Wednesday August 17, 2022 @03:55PM (#32500954)
I believe idioms for dealing with golang channels resemble NIO channels/selectors (e.g. https://docs.oracle.com/javase/8/docs/api/java/nio/channels/...). JDK1.4 was already widely used around 2005 ;)
Re:Learning a new language, or how I gained familiarity with Go by morelisp on Wednesday August 17, 2022 @04:32PM (#32501345)
The idioms for Go channels as well as NIO channels resemble those of any asynchronous file descriptor handling. Long predating 2005. (And more to the point, still relatively uncommon outside actual I/O in Java.)
Re:Learning a new language, or how I gained familiarity with Go by 62951413 on Wednesday August 17, 2022 @04:01PM (#32501037)
I cannot help thinking that golang's error handling is isomorphic to

* returning an error code from functions in C and all the time writing "err = f(); if (err != 0) .."

* returning a potentially null reference from a method in Java and all the time writing "var ref = f(); if (ref != null) .."

Exceptions were supposed to save us from this drudgery in late 90s. They did if you ask me. Options are a nice alternative if FP is your cup of tea.

Re:Learning a new language, or how I gained familiarity with Go by latchkey on Wednesday August 17, 2022 @12:31PM (#32498485)
As an old Java head, I found FX [1] to fill a missing DI gap for me. Some can argue that it isn't really needed in go, and I tend to agree, but after using it in a few large projects, I find that it encourages a good separation of concerns that enables things to be easier tested as well as prevents cyclical imports.

[1] https://github.com/uber-go/fx

Re:Learning a new language, or how I gained familiarity with Go by arwineap on Wednesday August 17, 2022 @03:54PM (#32500947)
I've found that fx is very popular with java devs

Can you help me understand what this style of dependency injection solves for in java?

I use packages to encourage separation of concerns

Re:Learning a new language, or how I gained familiarity with Go by latchkey on Wednesday August 17, 2022 @04:21PM (#32501217)
If you start to write pkg.NewFoo() inside of functions, you get into a whole object dependency mess that makes testing really really difficult. Having something that encourages you to pass in constructed objects (ala: DI), in the constructor, it ends up being much easier because you can more easily mock objects as necessary. Obviously, you can use that same pattern on your own without fx, but going all in on fx really kind of forces you to do it.

Bonus fx points for being able to start/stop services as well. However, this tends to factor more into single binary apps (which is what I've built), more than say restful web endpoints (also built, but don't use this feature of fx as much).

Re:Learning a new language, or how I gained familiarity with Go by randomdata on Wednesday August 17, 2022 @05:57PM (#32502237)
> Obviously, you can use that same pattern on your own without fx, but going all in on fx really kind of forces you to do it.

Unless you are trying to add tests to legacy code, where you also wouldn't have much say in the use of fx, wouldn't the stated testing difficultly already force you away from that dependency mess?

Re:Learning a new language, or how I gained familiarity with Go by latchkey on Wednesday August 17, 2022 @06:21PM (#32502474)
I've found that unless I write code in golang in a way that was intentionally written to be unit tested, it is very hard to bolt on unit testing after the fact. I'm stuck with effectively just integration testing. I don't envy you working on legacy code.

  struct Foo {
  }

  func (f *Foo) doSomething() {
     client := pkg.NewClient()
     client.doHttpRequest()
  }
vs.

  struct Foo {
    client Client
  }

  func (f *Foo) doSomething() {
     f.client.doHttpRequest()
  }

  func NewFoo(client Client) *Foo {
    return &Foo{ client: client }
  }

  var FooModule = fx.Provide(NewFoo)
then to use it...

  struct Bar {
    foo *Foo
  }

  func NewBar(foo *Foo) *Bar {
    return &Bar{foo: foo}
  }

  var BarModule = fx.Provide(NewBar)
how do you unit test the first example? There is no easy way to that without having the client do things special internally, which leads to a rats nest of issues and doesn't scale. Easier to just pass in a mock client and make sure the right http request/responses are being done.

fx kind of highly encourages the second approach for all of your code. It is a bit more boilerplate, but by default, I'm always going to be thinking about passing my dependencies around instead of just being lazy and doing NewClient in the function.

I also get to take advantage of the fact that Client is just magically passed into my constructor. If there is some error doing so (like I forget to Provide), then fx will give me errors at start up. My dependency chain is clear. It just feels nicer to me.

Once again, of course I can write it the second way without fx... but it is kind of nice that every time I want to use Foo, I don't have to type NewFoo() in a constructor... I just reference *Foo.

By the way, mockery [1] generates super easy to use mocks for exactly this workflow.

[1] https://github.com/vektra/mockery

Re:Learning a new language, or how I gained familiarity with Go by randomdata on Wednesday August 17, 2022 @06:34PM (#32502604)
> I've found that unless I write code in golang in a way that was intentionally written to be unit tested, it is very hard to bolt on unit testing after the fact.

Absolutely. This is true in most, if not all, languages. But when you're in the driver's seat, able to make choices like whether or not to use fx, why would you bolt on testing after the fact?

Not much you can do about legacy code, if that's what you have to deal with, but when dealing with legacy code it is unlikely the authors used fx, so...

Re:Learning a new language, or how I gained familiarity with Go by latchkey on Wednesday August 17, 2022 @07:27PM (#32503148)
As you refactor the code to be more testable, you can add in fx. It doesn't have to be from the beginning.
Re:Learning a new language, or how I gained familiarity with Go by randomdata on Wednesday August 17, 2022 @07:43PM (#32503306)
If you refactor code to be more testable then it is testing that is forcing you, not fx.
Re:Learning a new language, or how I gained familiarity with Go by latchkey on Wednesday August 17, 2022 @11:33PM (#32504956)
Correct, I haven't argued any differently. You introduce fx in, start writing tests and it will convince the other developers you're working with that there is importance to how you build the codebase in such a way as to enable testing.
Re:Learning a new language, or how I gained familiarity with Go by randomdata on Thursday August 18, 2022 @04:43AM (#32506919)
Then you must have mistakenly replied to the wrong thread? If you are in a position where you can introduce fx, then you are also in a position to introduce testing, and testing will already force testable code. If fx can help your code, great, but that's orthogonal to the discussion.
Re:Learning a new language, or how I gained familiarity with Go by kubb on Wednesday August 17, 2022 @01:49PM (#32499523)
you're right, fortunately stopping to use a concept is easier than learning a new one
Re:Learning a new language, or how I gained familiarity with Go by tylerhou on Wednesday August 17, 2022 @06:35PM (#32502619)
A big new concept in Go is that methods can have value receivers or pointer receivers. In C++ and Java, there is no distinction. Plus, method calls automatically deref pointers.

I think it took me a day to realize this when my code wasn't compiling.

That, and goroutines + channels (especially blocking behavior).

Re:Learning a new language, or how I gained familiarity with Go by ryanschneider on Wednesday August 17, 2022 @03:44PM (#32500832)
The first time I returned the address of a local variable from a function and it _wasn’t_ a footgun was eye opening. Then digging into why that actually worked and understanding how go does escape analysis gave me more respect for the language and it’s compiler.
Re:Learning a new language, or how I gained familiarity with Go by pram on Wednesday August 17, 2022 @01:04PM (#32498932)
The hard part about learning new languages for me is purging all the old information I have. For example, Perl sigils messed me up for a long time because other languages don't prefix variables and stuff with a symbol. It felt so wrong!
Re:Learning a new language, or how I gained familiarity with Go by rad_gruchalski on Wednesday August 17, 2022 @02:14PM (#32499784)
PHP does. Not the the extent of Perl, but it does.
Re:Learning a new language, or how I gained familiarity with Go by Keyframe on Wednesday August 17, 2022 @02:46PM (#32500146)
MEL does as well. Tcl too (sometimes).
Re:Learning a new language, or how I gained familiarity with Go by icemanx on Wednesday August 17, 2022 @05:16PM (#32501838)
So a Java and C++ Developer will know what a TEE channel is?