随笔分类 - Go
摘要:New is alernate way to create a variable new() function create a variable and returns a pointer to the variable Variable is initialized to zero packag
阅读全文
摘要:Write a MinMaxStack class for a Min Max Stack. The class should support: Pushing and popping values on and off the stack. Peeking at the value at the
阅读全文
摘要:// User type User struct { ID int FirstName string } // Method func (u *User) describe() string { desc := fmt.Sprintf("Name %s", u.FirstName) return d
阅读全文
摘要:package main import ( "fmt" ) func recoverFromPanic() { fmt.Println("defer") } func doThings() { defer recoverFromPanic() for i := 0; i < 5; i++ { fmt
阅读全文
摘要:package main import ( "errors" "fmt" "strings" ) func shouldGreaterThanTen(num int) error { if num < 10 { return errors.New("Number is less than 10")
阅读全文
摘要:func changeName(name *string) { *name = strings.ToUpper(*name) } // Coordinates type Coordinates struct { X, Y float64 } func main() { name := "Elvis"
阅读全文
摘要:var name string var namePointer *string // Pointer name = "Beyonce" namePointer = &name // Assign a Pointer fmt.Println("Name: ", name) fmt.Println("N
阅读全文
摘要:package main import "fmt" type User struct { ID int FirstName string LastName string Email string } func main() { u := User{ID: 1, FirstName: "Z", Las
阅读全文
摘要:file: utils/math.go package utils import ( "fmt" ) func printNum(num int) { fmt.Println("Current number is: ", num) } // Add multi int number together
阅读全文
摘要:Create a new folder utilswith a new file math.go package utils import ( "fmt" ) func printNum(num int) { fmt.Println("Current number is: ", num) } //
阅读全文
摘要:A Go map type looks like this: map[KeyType]ValueType This variable m is a map of string keys to int values: var m map[string]int Map types are referen
阅读全文
摘要:An array has a fixed size. A slice, on the other hand, is a dynamically-sized, flexible view into the elements of an array. In practice, slices are mu
阅读全文
摘要:Slices can be created with the built-in make function; this is how you create dynamically sized arrays. The make function allocates a zeroed array and
阅读全文
摘要:package main import ( "fmt" ) func main() { //var scores [5]float64 //fmt.Println(scores) // [0.0, 0.0,0.0,0.0,0.0] //var scores [5]float64 = [5]float
阅读全文
摘要:Function can return multi values: func printAge(age int) (int, int) { return 12, age } func main() { age1, age2 := printAge(8) fmt.Println(age1) fmt.P
阅读全文
摘要:There is no While loop in Go, but you can use For loop as while loop: i := 1 for i < 100 { fmt.Println(i) i += 1 } func main() { var myTxt = "This is
阅读全文
摘要:fallthrough keyword is used in switch statement in golang. This keyword is used in switch case block. If the fallthrough keyword is present in the cas
阅读全文
摘要:// Defined a err variable err and assige the return value of someFunction() // err only available in if {} block, not outside if err := someFunction()
阅读全文
摘要:package main import ( "fmt" ) func main() { var firstName string = "firstName" var lastName = "lastName" var fullName string // Go will assign default
阅读全文
摘要:Can use reflect.TypeOf to get variable type package main import ( "fmt" "reflect" ) func main() { // var age int = 21 // var b bool = age >= 23 var ag
阅读全文