Last active
August 29, 2015 14:04
-
-
Save mandulaj/1bd8936ffc96659d73ad to your computer and use it in GitHub Desktop.
Tour of Go
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 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)) | |
} |
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 "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) | |
} |
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 ( | |
"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) | |
} |
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 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)) | |
} |
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 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