Created by CyanHall.com on 11/12/2020 , Last updated: 03/30/2021.
πŸ‘‰Β Β github shields Star me if it’s helpful.

1. Hello

    // hello.go  The program entry main function must be in a code package named main.
package main

import (
	"fmt"
)

func main() {
	str := "world"
	text := fmt.Sprintf("hello %s! happy coding.", str)
	fmt.Println(text)
}
  

2. env

    go -h
go env

// run
go run hello.go
go build hello.go; ./hello
  

3. init

    package main

import "fmt"

func init() {
	fmt.Println("init 1")
}

func main() {
	fmt.Println("main")
}

func init() {
	fmt.Println("init 2")
}

// init 1
// init 2
// main
  

4. import

    import format "fmt" // format.Println()
import "fmt" // fmt.Println()
import . "fmt" // use Println directly, not recommended
import _ "net/http/pprof" // used to load pprof package, it's init function will be called.
  

5. JSON

    type Config struct {
    Name bool `json:"name"` // OK

    Name bool `json: "name"` // Error
    Name bool `json:name` // Error
}
  

6. String

    import (
    "strings"
)
strings.Contains("something", "some") // true
  

7. Array

    var number_array [3]int // [0, 0, 0]
append(number_array, 1) // Error
number_array[0] = 1 // [1, 0, 0]
  

8. Slice

    var number_slice []int // []
// Recreate to append
number_slice = append(number_slice, 1) // [1]
// Create a new slice from an array
some_numbers := number_array[0:1] // [0]
  

9. Map

    your_map := make(map[string]int)
your_map["key"] = 1
fmt.Println(your_map["key"]) // 1
// Remove key
delete(elements, "key")
  

10. Loop

    names := []string{"a", "b", "c"} // [a b c]
for i, name := range names {
	fmt.Printf("%d. %s
", i+1, name)
}
// 1. a
// 2. b
// 3. c
  

11. Interface

    // res.Data is <interface {}>, res.Data > data is <[]interface {}>
items = res.Data.([]interface{})
for i, item := range response {
  data := item.(map[string]interface{})
  data["id"].(float64)
}
  

12. Debug using Delve

    # https://github.com/go-delve/delve
dlv debug app.go
  

13. Inside Debugger

    # Set breakpoint
break [path/filename].go:[line_num]

# Run and should pauses at the breakpoint
continue

# Print variable
print [variable_name]

# Move to next line in the source
next
  

14. Gin

    // Read Request Body in JSON
type GitHubInput struct {
	Zen string `json:"zen"`
}
var input GitHubInput
if err := c.ShouldBindJSON(&input); err != nil {
	c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	return
}
input.Zen

// Read Header
c.Request.Header.Get("X-GitHub-Event")

// HTTP Response
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
c.String(200, input.Zen)
  

1. Hello

    // hello.go  The program entry main function must be in a code package named main.
package main

import (
	"fmt"
)

func main() {
	str := "world"
	text := fmt.Sprintf("hello %s! happy coding.", str)
	fmt.Println(text)
}
  

3. init

    package main

import "fmt"

func init() {
	fmt.Println("init 1")
}

func main() {
	fmt.Println("main")
}

func init() {
	fmt.Println("init 2")
}

// init 1
// init 2
// main
  

5. JSON

    type Config struct {
    Name bool `json:"name"` // OK

    Name bool `json: "name"` // Error
    Name bool `json:name` // Error
}
  

7. Array

    var number_array [3]int // [0, 0, 0]
append(number_array, 1) // Error
number_array[0] = 1 // [1, 0, 0]
  

9. Map

    your_map := make(map[string]int)
your_map["key"] = 1
fmt.Println(your_map["key"]) // 1
// Remove key
delete(elements, "key")
  

11. Interface

    // res.Data is <interface {}>, res.Data > data is <[]interface {}>
items = res.Data.([]interface{})
for i, item := range response {
  data := item.(map[string]interface{})
  data["id"].(float64)
}
  

13. Inside Debugger

    # Set breakpoint
break [path/filename].go:[line_num]

# Run and should pauses at the breakpoint
continue

# Print variable
print [variable_name]

# Move to next line in the source
next
  

2. env

    go -h
go env

// run
go run hello.go
go build hello.go; ./hello
  

4. import

    import format "fmt" // format.Println()
import "fmt" // fmt.Println()
import . "fmt" // use Println directly, not recommended
import _ "net/http/pprof" // used to load pprof package, it's init function will be called.
  

6. String

    import (
    "strings"
)
strings.Contains("something", "some") // true
  

8. Slice

    var number_slice []int // []
// Recreate to append
number_slice = append(number_slice, 1) // [1]
// Create a new slice from an array
some_numbers := number_array[0:1] // [0]
  

10. Loop

    names := []string{"a", "b", "c"} // [a b c]
for i, name := range names {
	fmt.Printf("%d. %s
", i+1, name)
}
// 1. a
// 2. b
// 3. c
  

12. Debug using Delve

    # https://github.com/go-delve/delve
dlv debug app.go
  

14. Gin

    // Read Request Body in JSON
type GitHubInput struct {
	Zen string `json:"zen"`
}
var input GitHubInput
if err := c.ShouldBindJSON(&input); err != nil {
	c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	return
}
input.Zen

// Read Header
c.Request.Header.Get("X-GitHub-Event")

// HTTP Response
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
c.String(200, input.Zen)
  


Maitained byΒ Cyanhall.com, Copy Rights @ CC BY-NC-SA 4.0