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 Sqrt(x float64) (z float64) { | |
z = 1.0 | |
for i := 0; i < 20; i++ { | |
z = z - (z * z - x) / (2 * z) | |
} | |
return z | |
} |
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
import "fmt" | |
type IPAddr [4]byte | |
func (this IPAddr) String() string { | |
return fmt.Sprintf("%d.%d.%d.%d", this[0], this[1], this[2], this[3]) | |
} |
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
import ( | |
"io" | |
) | |
type rot13Reader struct { | |
r io.Reader | |
} | |
func (reader *rot13Reader) Read(param []byte) (int, error) { | |
si, err := reader.r.Read(param) |
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
type MyReader struct{} | |
func (m MyReader) Read(param []byte) (int, error) { | |
count := 0 | |
for i := 0; i < len(param); i++ { | |
param[i] = 'A' | |
count += 1 | |
} | |
return count, 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
import ( | |
"strings" | |
) | |
func WordCount(s string) (container map[string]int) { | |
fields := strings.Fields(s) | |
container = make(map[string]int) | |
for i := 0; i < len(fields); i++ { | |
if _, ok := container[fields[i]]; ok == true { |
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
import "image" | |
import "image/color" | |
type Image struct{ | |
x int | |
y int | |
} | |
func (im Image) Bounds() (image.Rectangle) { | |
return image.Rect(0, 0, im.x, im.y) |
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 fibonacci() func() int { | |
a, b := -1, 1 | |
return func() int { | |
if a == -1 { | |
a = 0 | |
return a | |
} | |
a, b = b, a + b | |
return a | |
} |
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
import ( | |
"fmt" | |
) | |
type ErrNegativeSqrt float64 | |
func (e ErrNegativeSqrt) Error() string { | |
return fmt.Sprintf("cannot Sqrt negative number: %d", int(e)) | |
} |
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 Pic(dx, dy int) [][]uint8 { | |
pic := make([][]uint8, dy) | |
for i := 0; i < dy; i++ { | |
pic[i] = make([]uint8, dx) | |
for u := 0; u < dx; u++ { | |
pic[i][u] = uint8((u + i) / 2) | |
} | |
} | |
return pic |