Skip to content

Instantly share code, notes, and snippets.

@mandulaj
Last active August 29, 2015 14:04
Show Gist options
  • Save mandulaj/1bd8936ffc96659d73ad to your computer and use it in GitHub Desktop.
Save mandulaj/1bd8936ffc96659d73ad to your computer and use it in GitHub Desktop.
Tour of Go
package main
import (
"fmt"
)
func Sqrt(x float64) float64 {
z:= x
for i:=0;i<10000;i++ {
z=z-(z*z-x)/(2*z)
}
return z
}
func main() {
fmt.Println(Sqrt(2))
}
package main
import "code.google.com/p/go-tour/pic"
func Exp(x, y int) int{
return x^y
}
func Pic(dx, dy int) [][]uint8 {
y := make([][]uint8, dy)
for i := range y {
y[i] = make([]uint8, dx)
for j:=range y[i] {
y[i][j] = uint8(Exp(i,j))
}
}
return y
}
func main() {
pic.Show(Pic)
}
package main
import (
"code.google.com/p/go-tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
words := make(map[string]int)
wl := strings.Fields(s)
for i := range wl {
ele := words[wl[i]]
words[wl[i]] = ele+1
}
return words
}
func main() {
wc.Test(WordCount)
}
package main
import "fmt"
func Cbrt(x complex128) complex128 {
z:= x
for i := 0;i<100000;i++ {
z=z-((z*z*z-x)/(3*z*z))
}
return z
}
func main() {
fmt.Println(Cbrt(2))
}
package main
import (
"fmt"
)
type ErrNegativeSqrt float64
func (e ErrNegativeSqrt) Error() string {
return fmt.Sprintf("cannot Sqrt negative number: %f", float64(e))
}
func Sqrt(f float64) (float64, error) {
z:=f
if f<0 {
return f, ErrNegativeSqrt(f)
}
for i:=0;i<100000;i++ {
z=z-(z*z-f)/(2*z)
}
return z, nil
}
func main() {
fmt.Println(Sqrt(2))
fmt.Println(Sqrt(-2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment