Last active
August 29, 2015 13:56
-
-
Save javouhey/9234850 to your computer and use it in GitHub Desktop.
#golang Stupid mistakes newbies make
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A. Cannot use type switch outside of a switch statement | |
type PolarVortex interface {} | |
func main() { | |
var p PolarVortex = new(PolarVortex) | |
var s = p.(type) // compilation error => use of .(type) outside type switch | |
} | |
// B. type assertion | |
var p PolarVortex = new(PolarVortex) | |
s := p.(PolarVortex) | |
// C. new of a primitive returns a pointer | |
i := new(int) | |
*i = 0 | |
// D. python & javascript single or double quoted strings | |
k := "hello" | |
m := '>' + k + '<' // rune(62) + k (mismatched types rune and *string) | |
// E: 3 ways to get a pointer to a struct type | |
type A struct { a int } | |
a := new(A) | |
b := &A{} | |
c1 := A{}; c2:= &c1 | |
fmt.Println("output", a, b, c2) // prints "output &{0} &{0} &{0}" | |
// F: Every package can declare an init function for execution | |
// Real world go -> https://github.com/go-sql-driver/mysql/blob/master/utils.go#L33 | |
func init() { ... } // See http://golang.org/ref/spec#Program_initialization_and_execution |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment