Skip to content

Instantly share code, notes, and snippets.

@mebubo
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save mebubo/ff770f5faff56bc0ea86 to your computer and use it in GitHub Desktop.

Select an option

Save mebubo/ff770f5faff56bc0ea86 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
)
type ErrNegativeSqrt float64
func (e ErrNegativeSqrt) Error() string {
return fmt.Sprintf("cannot Sqrt negativ number: %g", float64(e))
}
func Sqrt(x float64) (float64, error) {
if x < 0 {
return x, ErrNegativeSqrt(x)
}
z := 1.0
for !goodEnough(x, z) {
z = improveGuess(x, z)
}
return z, nil
}
func improveGuess(x, z float64) float64 {
return z - (z*z - x) / (2 * z)
}
func goodEnough(x, z float64) bool {
return math.Abs(z*z - x) / x < 1e-15
}
func main() {
fmt.Println(Sqrt(2))
fmt.Println(Sqrt(-2))
}
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
curr := 1
prev := 1
return func() int {
prev, curr = curr, curr + prev
return prev
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
package main
import (
"net/http"
"fmt"
)
type String string
type Struct struct {
Greeting, Punct, Who string
}
func (s String) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, s)
}
func (s Struct) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s%s%s", s.Greeting, s.Punct, s.Who)
}
func main() {
http.Handle("/string", String("Hello"))
http.Handle("/struct", &Struct{"Hello", ", ", "World"})
http.ListenAndServe("localhost:4000", nil)
}
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
z := 1000.0
count := 0
for !goodEnough(x, z) {
z = improveGuess(x, z)
count++
}
fmt.Println(count)
return z
}
func improveGuess(x, z float64) float64 {
return z - (z*z - x) / (2 * z)
}
func goodEnough(x, z float64) bool {
return math.Abs(z*z - x) / x < 0.00000000000001
}
func main() {
fmt.Println(Sqrt(5))
fmt.Println(math.Sqrt(5))
}
package main
import (
"fmt"
"math/cmplx"
)
func Cbrt(x complex128) complex128 {
z := complex128(1.0)
count := 0
for !goodEnough(x, z) {
z = improveGuess(x, z)
count++
}
fmt.Println(count)
return z
}
func improveGuess(x, z complex128) complex128 {
return z - (cmplx.Pow(z, 3) - x) / (3 * cmplx.Pow(z, 2))
}
func goodEnough(x, z complex128) bool {
return (cmplx.Abs(cmplx.Pow(z, 3) - x) / cmplx.Abs(x)) < 0.00001
}
func main() {
fmt.Println(Cbrt(2i))
}
package main
import "code.google.com/p/go-tour/pic"
func Pic(dx, dy int) [][]uint8 {
result := make([][]uint8, dy)
for i := range result {
result[i] = make([]uint8, dx);
for j := range result[i] {
result[i][j] = uint8(i^j)
}
}
return result
}
func main() {
pic.Show(Pic)
}
package main
import (
"code.google.com/p/go-tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
result := make(map[string]int)
for _, word := range strings.Fields(s) {
_, ok := result[word]
if ok {
result[word]++
} else {
result[word] = 1
}
}
return result
}
func main() {
wc.Test(WordCount)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment