arda, 6 months ago Just started learning Go! Another language for my tech stack. Also, any tips for a go newbie, but a seasoned engineer is appreciated. #golang #go #gopher
Just started learning Go!
Another language for my tech stack.
Also, any tips for a go newbie, but a seasoned engineer is appreciated.
#golang #go #gopher
reiver, 6 months ago @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
"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, 6 months ago @reiver Woah, thanks for the extended tips and tricks. I'll make sure I'll check them all one by one! 🚀
@reiver Woah, thanks for the extended tips and tricks. I'll make sure I'll check them all one by one! 🚀
reiver, 6 months ago (edited 6 months ago) @arda The built-in logging library in Golang sucks. You will probably implement your own eventually, or use someone else's logging package.
The built-in logging library in Golang sucks.
You will probably implement your own eventually, or use someone else's logging package.
reiver, 6 months ago @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.)
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, 6 months ago @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.
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, 6 months ago @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.
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, 6 months ago @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) { // ... }
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, 6 months ago @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 } // ... }
The "t" parameter is used to output from your test.
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, 6 months ago @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 .
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, 6 months ago @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) .
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)
Add comment