DEV Community

Cover image for Ultimate GoLang Beginner's Cheat Sheet
Toul
Toul

Posted on

Ultimate GoLang Beginner's Cheat Sheet

When I first learned GoLang 5 years ago, I remember constantly looking up its syntax and things like printing, which was much different than Python 3's way of doing things.

So, I've put together the things I looked up the most during my first few months with GoLang in a handy cheat sheet.

ultimate-golang-beginners-cheat-sheet

But don't worry, I'll provide it in text for those who like blog posts.

Control Flow

switch

The GoLang switch is a little different in that it must always have a default value.

t := time.Now() switch { case t.Hour() < 12: fmt.Println("It's before twelve") default: fmt.Println("It's after twelve") } 
Enter fullscreen mode Exit fullscreen mode

else if

There's no elif here, like in Python.

You have to write it out.

// else if if num := 0; num < 0 { fmt.Println(num, "is negative") } else if num < 10 { fmt.Printf("%d has one digit", num) } else { fmt.Println(num, "has multiple digits") } 
Enter fullscreen mode Exit fullscreen mode

Conversions

There's no int() or str(), but here are a few ways of going about it. Personally, when going to string, I tend to use Sprintf

golang bytes array to string

s := string([]byte{65, 66, 67, 226, 130, 172}) fmt.Println(s) 
Enter fullscreen mode Exit fullscreen mode

golang bool to string

var b bool = true fmt.Println(reflect.TypeOf(b)) 
Enter fullscreen mode Exit fullscreen mode

strconv

var S string = strconv.FormatBool(true) fmt.Println(reflect.TypeOf(S)) 
Enter fullscreen mode Exit fullscreen mode

Sprintf

B := true str := fmt.Sprintf("%v", B) fmt.Println(str) fmt.Println(reflect.TypeOf(s)) 
Enter fullscreen mode Exit fullscreen mode

golang string to int

strVar := "100" intVar, err := strconv.Atoi(strVar) fmt.Println(intVar, err, reflect.TypeOf(intVar)) 
Enter fullscreen mode Exit fullscreen mode

int to string

i := 10 s1 := strconv.FormatInt(int64(i), 10) s2 := strconv.Itoa(i) fmt.Printf("%v, %v\n", s1, s2) 
Enter fullscreen mode Exit fullscreen mode

golang while Loops

In GoLang there is no while keyword, only for, so to emulate the behavior of while-loops, here's how it is done.

Like a for loop

// while i := 0 for i < 10 { // ... } 
Enter fullscreen mode Exit fullscreen mode

Endless loop

// Endless while loop for { // ... } 
Enter fullscreen mode Exit fullscreen mode

While true

// while true for true { // ... } 
Enter fullscreen mode Exit fullscreen mode

do-while

// do-while for { if !condition { break } } 
Enter fullscreen mode Exit fullscreen mode

golang Print a Struct

Thankfully there's built-in struct printing from the standard library.

// Printing struct type employee struct { name string age int salary int } emp := &employee{ name: "Toul", age: 24, salary: 500000, // a writer can dream } 
Enter fullscreen mode Exit fullscreen mode

Print without fields

fmt.Printf("%v", emp) // {Toul 24 500000} 
Enter fullscreen mode Exit fullscreen mode

Print with fields

fmt.Printf("%+v", emp) // {name:Toul age:24 salary:500000} 
Enter fullscreen mode Exit fullscreen mode

Conclusion

Hopefully, these help you on your GoLang journey!

Top comments (1)

Collapse
 
marcello_h profile image
Marcelloh

see:
fmt.Printf("%#v", emp)