Skip to content

Instantly share code, notes, and snippets.

@tenntenn
Created October 13, 2018 06:11
Show Gist options
  • Save tenntenn/8cb7515cfa0c41b5c4cd69fb7977c0d3 to your computer and use it in GitHub Desktop.
Save tenntenn/8cb7515cfa0c41b5c4cd69fb7977c0d3 to your computer and use it in GitHub Desktop.
ゼロ値を使おう #golang ref: https://qiita.com/tenntenn/items/c55095585af64ca28ab5
package main
import "fmt"
func main() {
var n int
var f float64
var s string
var b bool
fmt.Printf("%#v %#v %#v %#v", n, f, s, b)
}
0 0 "" false
package main
import "fmt"
func main() {
var f func()
var ptr *int
fmt.Printf("%#v %#v", f, ptr)
}
(func())(nil) (*int)(nil)
prog.go:8: Printf format %#v arg f is a func value, not called
package main
import "fmt"
func main() {
count := 0
count++
count++
fmt.Println(count)
}
package main
import "fmt"
func main() {
var count int
count++
count++
fmt.Println(count)
}
package main
import "fmt"
func main() {
words := []string{"dog", "cat", "dog", "fish", "cat"}
wc := map[string]int{}
for _, w := range words {
wc[w]++
}
fmt.Println(wc)
}
map[dog:2 cat:2 fish:1]
package main
import "fmt"
func main() {
words := []string{"dog", "cat", "dog", "fish", "cat"}
wc := map[string]bool{}
for _, w := range words {
wc[w] = true
}
for _, w := range []string{"dog", "pig"} {
if wc[w] {
fmt.Println(w, "は存在する")
} else {
fmt.Println(w, "は存在しない")
}
}
}
package main
import "fmt"
func main() {
var ns []int
fmt.Printf("%#v %d %d\n", ns, len(ns), cap(ns))
ns = append(ns, 10, 20)
fmt.Println(ns)
}
[]int(nil) 0 0
[10 20]
package main
import "fmt"
func main() {
var ns [3]int
fmt.Println(ns)
}
package main
import "sync"
func main() {
var mu sync.Mutex
mu.Lock()
mu.Unlock()
}
package main
import (
"fmt"
"sync"
)
func main() {
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
fmt.Println("done 1")
}()
go func() {
defer wg.Done()
fmt.Println("done 1")
}()
wg.Wait()
fmt.Println("main done")
}
package main
import "fmt"
type WordCounter struct {
m map[string]int
}
func NewWordCounter() *WordCounter {
return &WordCounter{
m: map[string]int{},
}
}
func (wc *WordCounter) Count(s string) int {
wc.m[s]++
return wc.m[s]
}
func (wc *WordCounter) Get(s string) int {
return wc.m[s]
}
func main() {
wc := NewWordCounter()
words := []string{"dog", "cat", "dog", "fish", "cat"}
for _, w := range words {
wc.Count(w)
}
fmt.Println("dog", wc.Get("dog"))
}
func (wc *WordCounter) Count(s string) int {
if wc.m == nil {
wc.m = map[string]int{}
}
wc.m[s]++
return wc.m[s]
}
package main
import (
"fmt"
"sync"
)
type WordCounter struct {
initOnce sync.Once
m map[string]int
}
func (wc *WordCounter) init() {
wc.m = map[string]int{}
}
func (wc *WordCounter) Count(s string) int {
wc.initOnce.Do(wc.init)
wc.m[s]++
return wc.m[s]
}
func (wc *WordCounter) Get(s string) int {
wc.initOnce.Do(wc.init)
return wc.m[s]
}
func main() {
var wc WordCounter
words := []string{"dog", "cat", "dog", "fish", "cat"}
for _, w := range words {
wc.Count(w)
}
fmt.Println("dog", wc.Get("dog"))
}
func main() {
var wc WordCounter
words := []string{"dog", "cat", "dog", "fish", "cat"}
for _, w := range words {
wc.Count(w) // (&wc).Count(w)と同様
}
fmt.Println("dog", wc.Get("dog")) // (&wc).Get("dog")と同様
}
var buf bytes.Buffer
// bufはio.Writerを実装していないからコンパイルエラー
fmt.Fprintf(buf, "Hello")
// &bufはio.Writerを実装している
fmt.Fprintf(&buf, "Hello")
package main
import "fmt"
func main() {
type Hoge struct {
N int
NS [3]int
}
var h Hoge
fmt.Printf("%#v", h)
}
main.Hoge{N:0, NS:[3]int{0, 0, 0}}
package main
import "fmt"
func main() {
type Hoge struct {
N int
}
type Fuga struct {
int
Hoge
}
var f Fuga
fmt.Printf("%#v", f)
}
main.Fuga{int:0, Hoge:main.Hoge{N:0}}
package main
import "fmt"
func main() {
var ch chan int
var ns []int
var m map[string]int
fmt.Printf("%#v %#v %#v", ch, ns, m)
}
(chan int)(nil) []int(nil) map[string]int(nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment