arda,
@arda@micro.arda.pw avatar

Just started learning Go!

Another language for my tech stack.

Also, any tips for a go newbie, but a seasoned engineer is appreciated.

reiver,
@reiver@mastodon.social avatar

@arda

"defer" is a useful keyword.

It lets you specify code to run whenever and wherever a function ends.

...

Here is an example:

func DoStuff(filename string) error {

// ...

file, err := os.Open(filename)
if nil != err {
return err
}
defer file.Close()

// ...

}

...

It lets the cleanup code be near the creation code.

arda,
@arda@micro.arda.pw avatar

@reiver Woah, thanks for the extended tips and tricks. I'll make sure I'll check them all one by one! 🚀

reiver, (edited )
@reiver@mastodon.social avatar

@arda

The built-in logging library in Golang sucks.

You will probably implement your own eventually, or use someone else's logging package.

reiver,
@reiver@mastodon.social avatar

@arda

If you have trouble downloading the latest version of a golang package, then use GOPROXY=direct

...

For example, if this command is what is giving you the problem:

go get https://example·com/path

Then try:

GOPROXY=direct go get https://example·com/path

(This should bypass the golang team's caching server.)

reiver,
@reiver@mastodon.social avatar

@arda

You don't have to use gofmt if you don't want to.

...

gofmt usually breaks inter-line alignment — so there are times you might not want to use it.

reiver,
@reiver@mastodon.social avatar

@arda

With Golang, you do not need a separate web-server.

Your Golang program is its own web-server.

...

Golang has a built-in package for this —

import "net/http"

But others have also implemented HTTP themselves. And you could alternatively use those.

reiver,
@reiver@mastodon.social avatar

@arda

Golang has built-in support for unit-tests.

You just create a file that ends in "_test.go"

For example:

• apple_test.go
• banana_test.go
• cherry_test.go

...

You run:

go test

... from the terminal command line to run your tests.

...

Inside those test files, your tests need to be in specially named functions.

The function name needs to start with "Test". For example:

func TestSomething(t *testing.T) {
// ...
}

func TestThat(t *testing.T) {
// ...
}

reiver,
@reiver@mastodon.social avatar

@arda

The "t" parameter is used to output from your test.

For example:

func TestSomething(t *testing.T) {
// ...

if nil != err {
t.Errorf("Did not expect an error but actually got one.")
t.Logf("err = (%T) %s", err)
continue
}

// ...
}

reiver,
@reiver@mastodon.social avatar

@arda

Golang doesn't have a built-in package for WebSockets.

But — this is what most people use for WebSockets:

https://github.com/gorilla/websocket

.

reiver,
@reiver@mastodon.social avatar

@arda

For debugging with golang:

...

This will print out a variable's type:

fmt.Printf("something = %T \n", something)

(There are times when it isn't clear what a variable's type is. )

...

And, this will print out a variable's value:

fmt.Printf("something = %#v \n", something)

.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • golang
  • DreamBathrooms
  • everett
  • osvaldo12
  • magazineikmin
  • thenastyranch
  • rosin
  • normalnudes
  • Youngstown
  • Durango
  • slotface
  • ngwrru68w68
  • kavyap
  • mdbf
  • InstantRegret
  • JUstTest
  • ethstaker
  • GTA5RPClips
  • tacticalgear
  • Leos
  • anitta
  • modclub
  • khanakhh
  • cubers
  • cisconetworking
  • megavids
  • provamag3
  • tester
  • lostlight
  • All magazines