My Go Snippets
- go-snippets
- TOC
- useful links
- hello world
- read values from stdin
- read line from stdin
- convert string to int
- convert int to string
- count characters in string
- index character in string
- reverse string
- print current time
- max find from array or slice
- generate random number
- reversing slice
- trim line feed
- json encoding, decoding
- goroutine in for loop
- generate uuidv4
- call sts get-caller-identity with debug log using aws-sdk-go
package main
import "fmt"
func main() {
fmt.Println("Hello, World")
}
package main
import (
"fmt"
)
func main() {
var s1, s2 string
fmt.Scan(&s1, &s2)
fmt.Println(s1, s2)
}
sample
% go run tmp.go
hoge fuga
hoge fuga
% go run tmp.go
hoge
fuga
hoge fuga
package main
import (
"bufio"
"fmt"
"os"
8000
span>
)
func main() {
var s string
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
s = scanner.Text()
}
fmt.Print(s)
}
sample
% go run tmp.go
hello world
hello world
package main
import (
"fmt"
"strconv"
)
func main() {
s := "777"
i, _ := strconv.Atoi(s)
fmt.Printf("%T", i)
}
package main
import (
"fmt"
"strconv"
)
func main() {
i := 777
s := strconv.Itoa(i)
fmt.Printf("%T", s)
}
package main
import (
"fmt"
"strings"
)
func main() {
s := "hello world!"
fmt.Print(strings.Count(s, ""))
}
go - How to index characters in a Golang string? - Stack Overflow
https://stackoverflow.com/questions/15018545/how-to-index-characters-in-a-golang-string
Strings, bytes, runes and characters in Go - The Go Programming Language
https://go.dev/blog/strings
The Go Programming Language Specification - The Go Programming Language
https://go.dev/ref/spec#Conversions
package main
import "fmt"
func main() {
fmt.Println(string("Hello"[1])) // ASCII only
fmt.Println(string([]rune("Hello, 世界")[1])) // UTF-8
fmt.Println(string([]rune("Hello, 世界")[8])) // UTF-8
}
How to reverse a string in Go? - Stack Overflow
https://stackoverflow.com/questions/1752414/how-to-reverse-a-string-in-go
package main
import "fmt"
func Reverse(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
func main() {
fmt.Println(Reverse("Hello"))
}
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println(time.Now())
}
func findMax(n ...int) int {
max := 0
for _, value := range n {
if value > max {
max = value
}
}
return max
}
func main() {
findMax(1, 3, 5)
}
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
fmt.Println(rand.Intn(10))
}
package main
import "fmt"
func main() {
a := []int{1, 2, 3, 4, 5, 6}
fmt.Println(a) // [1 2 3 4 5 6]
for i := len(a)/2 - 1; i >= 0; i-- {
opp := len(a) - 1 - i
a[i], a[opp] = a[opp], a[i]
}
fmt.Println(a) // [6 5 4 3 2 1]
}
SliceTricks · golang/go Wiki
https://github.com/golang/go/wiki/SliceTricks#reversing
package main
import (
"fmt"
"strings"
)
func main() {
rawText := "Hello\nWorld!"
text := strings.ReplaceAll(rawText, "\n", " ")
fmt.Println(text)
}
strings package - strings - pkg.go.dev
https://pkg.go.dev/strings#ReplaceAll
Marshal encoding => struct to json
package main
import (
"encoding/json"
"fmt"
)
type ColorGroup struct {
ID int
Name string
Colors []string
}
func main() {
myGroup := ColorGroup{
ID: 1,
Name: "Reds",
Colors: []string{
"Crimson",
"Red",
"Ruby",
"Maroon",
},
}
bytes, err := json.Marshal(myGroup)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(bytes))
}
Unmarshal decoding => json to struct
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type Response struct {
Page int `json:"page"`
PerPage int `json:"per_page"`
Total int `json:"total"`
TotalPages int `json:"total_pages"`
Data []struct {
ID int `json:"id"`
Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Avatar string `json:"avatar"`
} `json:"data"`
Support struct {
URL string `json:"url"`
Text string `json:"text"`
} `json:"support"`
}
func PrettyPrint(i interface{}) string {
s, _ := json.MarshalIndent(i, "", "\t")
return string(s)
}
func main() {
resp, err := http.Get("https://reqres.in/api/users?page=2")
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) // response body is []byte
var result Response
if err := json.Unmarshal(body, &result); err != nil { // Parse []byte to the go struct pointer
fmt.Println(err)
}
fmt.Println(PrettyPrint(result))
// fmt.Println(result)
// loop
// for _, data := range result.Data {
// fmt.Println(data.FirstName)
// }
}
Reqres - A hosted REST-API ready to respond to your AJAX requests
https://reqres.in/
json package - encoding/json - pkg.go.dev
https://pkg.go.dev/encoding/json
JSON and Go - The Go Programming Language
https://go.dev/blog/json
JSON-to-Go: Convert JSON to Go instantly
https://mholt.github.io/json-to-go/
bind local value
package main
import (
"fmt"
"time"
)
func main() {
values := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
for _, value := range values {
i := value
go func() {
fmt.Println(i)
}()
}
time.Sleep(1 * time.Second)
}
args in anonymous function
package main
import (
"fmt"
"time"
)
func main() {
values := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
for _, value := range values {
go func(i int) {
fmt.Println(i)
}(value)
}
time.Sleep(1 * time.Second)
}
isolate function
package main
import (
"fmt"
"time"
)
func main() {
values := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
myFunc := func(i int) {
fmt.Println(i)
}
for _, value := range values {
go myFunc(value)
}
time.Sleep(1 * time.Second)
}
package main
import (
"fmt"
"github.com/google/uuid"
)
func main() {
uuidv4, _ := uuid.NewRandom()
fmt.Print(uuidv4)
}
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
"fmt"
)
func main() {
sess, err := session.NewSession(&aws.Config{
Region: aws.String("ap-northeast-1"),
LogLevel: aws.LogLevel(aws.LogDebug),
})
svc := sts.New(sess)
input := &sts.GetCallerIdentityInput{}
result, err := svc.GetCallerIdentity(input)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(result)
}