Created
October 13, 2018 06:11
-
-
Save tenntenn/8cb7515cfa0c41b5c4cd69fb7977c0d3 to your computer and use it in GitHub Desktop.
ゼロ値を使おう #golang ref: https://qiita.com/tenntenn/items/c55095585af64ca28ab5
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
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) | |
} |
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
0 0 "" false |
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
package main | |
import "fmt" | |
func main() { | |
var f func() | |
var ptr *int | |
fmt.Printf("%#v %#v", f, ptr) | |
} |
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
(func())(nil) (*int)(nil) |
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
prog.go:8: Printf format %#v arg f is a func value, not called |
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
package main | |
import "fmt" | |
func main() { | |
count := 0 | |
count++ | |
count++ | |
fmt.Println(count) | |
} |
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
package main | |
import "fmt" | |
func main() { | |
var count int | |
count++ | |
count++ | |
fmt.Println(count) | |
} |
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
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) | |
} |
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
map[dog:2 cat:2 fish:1] |
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
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, "は存在しない") | |
} | |
} | |
} |
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
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) | |
} |
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
[]int(nil) 0 0 | |
[10 20] |
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
package main | |
import "fmt" | |
func main() { | |
var ns [3]int | |
fmt.Println(ns) | |
} |
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
package main | |
import "sync" | |
func main() { | |
var mu sync.Mutex | |
mu.Lock() | |
mu.Unlock() | |
} |
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
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") | |
} |
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
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")) | |
} |
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
dog 2 |
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
func (wc *WordCounter) Count(s string) int { | |
if wc.m == nil { | |
wc.m = map[string]int{} | |
} | |
wc.m[s]++ | |
return wc.m[s] | |
} |
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
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")) | |
} |
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
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")と同様 | |
} |
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
var buf bytes.Buffer | |
// bufはio.Writerを実装していないからコンパイルエラー | |
fmt.Fprintf(buf, "Hello") | |
// &bufはio.Writerを実装している | |
fmt.Fprintf(&buf, "Hello") |
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
[0 0 0] |
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
package main | |
import "fmt" | |
func main() { | |
type Hoge struct { | |
N int | |
NS [3]int | |
} | |
var h Hoge | |
fmt.Printf("%#v", h) | |
} |
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
main.Hoge{N:0, NS:[3]int{0, 0, 0}} |
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
package main | |
import "fmt" | |
func main() { | |
type Hoge struct { | |
N int | |
} | |
type Fuga struct { | |
int | |
Hoge | |
} | |
var f Fuga | |
fmt.Printf("%#v", f) | |
} |
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
main.Fuga{int:0, Hoge:main.Hoge{N:0}} |
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
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) | |
} |
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
(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