download:扛得住的MySQL数据库架构
如何获得MySQL最优性能?如何建立MySQL高可用集群?如何搭建稳定高效的MySQL环境?国内顶级电商公司数据库专家带你成为一名优秀的DBA。
适合人群
适合具有一定自学能力,掌握Linux系统基础知识及MySQL数据库基础知识的学员
-
-
c add1(x *Number) *Number {
-
e := new(Number)
-
*e = x
-
return e
-
}
-
func sub1(x *Number) *Number {
-
return *x
-
}
-
func add(x, y *Number) *Number {
-
if isZero(y) {
-
return x
-
}
-
return add(add1(x), sub1(y))
-
}
-
func mul(x, y *Number) *Number {
-
if isZero(x) || isZero(y) {
-
return zero()
-
}
-
return add(mul(x, sub1(y)), x)
-
}
-
func fact(n *Number) *Number {
-
if isZero(n) {
-
return add1(zero())
-
}
-
return mul(fact(sub1(n)), n)
-
}
-
// -------------------------------------
-
// Helpers to generate/count Peano integers
-
func gen(n int) *Number {
-
if n > 0 {
-
return add1(gen(n - 1))
-
}
-
return zero()
-
}
-
func count(x *Number) int {
-
if isZero(x) {
-
return 0
-
}
-
return count(sub1(x)) + 1
-
}
-
// -------------------------------------
-
// Print i! for i in [0,9]
-
func main() {
-
for i := 0; i <= 9; i++ {
-
f := count(fact(gen(i)))
-
fmt.Println(i, "! =", f)
-
}
-
}
这是一个 n!的例子,但又不是单单的一个求 n! 的例子;这个例子让 e 们赶脚到了 —— 自然数能够结构出来或是逐个数出来!“皮亚诺自然数公理”:0 是一个自然数;每个自然数后面都跟有一个自然数;除 0 之外,每个自然数前面都有一个自然数;—— 0 很特殊,是第一个自然数,由于它前面没有自然数!同时例子中也反映了:阶乘依赖于自然数乘法;自然数乘法依赖于自然数加法,而自然数加法其实是个递归定义。
自然数就是一个链表,链表的第一个元素代表 0 ,第二个元素代表 1,第三个 代表 2,不断下去。10 就是一个长度为 10 的链表!求 10!的思绪不再是循环一下,乘一下!而是,结构出一个长度是 10! 的链表,然后 count 一下长度,就是 10!多此一举?!这里一切的操作只树立在“加一”、“减一”和“递归”的根底之上。
-
-
并发求圆周率 π
-
// Concurrent computation of pi.
-
// See goo.gl/ZuTZM.
-
//
-
// This demonstrates Go's ability to handle
-
// large numbers of concurrent processes.
-
// It is an unreasonable way to calculate pi.
-
package main
-
import (
-
"fmt"
-
"math"
-
)
-
func main() {
-
fmt.Println(pi(5000))
-
}
-
// pi launches n goroutines to compute an
-
// approximation of pi.
-
func pi(n int) float64 {
-
ch := make(chan float64)
-
for k := 0; k <= n; k++ {
-
go term(ch, float64(k))
-
}
-
f := 0.0
-
for k := 0; k <= n; k++ {
-
f += <-ch
-
}
-
return f
-
}
-
func term(ch chan float64, k float64) {
-
ch <- 4 * math.Pow(-1, k) / (2*k + 1)
-
}
割圆术求圆周长或是圆周率,听的比拟多,but,太不实在可行!不论公式怎样来的,总之有下面一个公式,本例子就是用这个公式求 π :

例子中运用了并发求每单项的值,然后将每单项的值加起来,赶脚是,加的项数越多,便越接近与真值。
go 中的管道(channel)提供了一种很便当的并发同步机制。管道是衔接多个并发函数的通道,就像是水管,一端能够流进去,另一端能够流进来。这里多个函数/进程将每个单项的求值放到管道之后,谁先谁后是不肯定的,然后另外一个函数/进程从管道中取值然后加起来,最后的结果便是就得的 π 的近似值。管道的同步操作曾经内置,也就是不需求偶们本人去管来的。 go 是偶目前看到的写并发程序最容易的言语,么有之一!
-
-
并发经过挑选法求素数
-
// A concurrent prime sieve
-
package main
-
// Send the sequence 2, 3, 4, ... to channel 'ch'.
-
func Generate(ch chan<- int) {
-
for i := 2; ; i++ {
-
ch <- i // Send 'i' to channel 'ch'.
-
}
-
}
-
// Copy the values from channel 'in' to channel 'out',
-
// removing those divisible by 'prime'.
-
func Filter(in <-chan int, out chan<- int, prime int) {
-
for {
-
i := <-in // Receive value from 'in'.
-
if i%prime != 0 {
-
out <- i // Send 'i' to 'out'.
-
}
-
}
-
}
-
// The prime sieve: Daisy-chain Filter processes.
-
func main() {
-
ch := make(chan int) // Create a new channel.
-
go Generate(ch) // Launch Generate goroutine.
-
for i := 0; i < 10; i++ {
-
prime := <-ch
-
print(prime, "\n")
-
ch1 := make(chan int)
-
go Filter(ch, ch1, prime)
-
ch = ch1
-
}
-
}
-
挑选法找素数是个不错的办法,2、3、4、5、6、7、8、9、10 ... 这些数中,第一个数 2 是素数,取出来,然后将 2 的倍数全部去掉;剩下的第一个数 3 还是素数,再去掉一切 3 的倍数,不断停止下去,就能找到很多素数。本例子也就是用的这个办法。
详细逻辑是:第一个管道记载从2开端的自然数,取第一自然数/质数 2;然后第二个管道记载从第一个管道中过滤后的一切自然数,再取一个质数 3;第三个管道取第二个管道中过滤后的数,又得一个质数;一切的管道都是无限长的,只需程序嚒有终止,这些挑选/过滤便不断在停止着。
-
孔明棋
-
// This program solves the (English) peg
-
// solitaire board game.
-
package main
-
import "fmt"
-
const N = 11 + 1 // length of a row (+1 for \n)
-
// The board must be surrounded by 2 illegal
-
// fields in each direction so that move()
-
// doesn't need to check the board boundaries.
-
// Periods represent illegal fields,
-
// ● are pegs, and ○ are holes.
-
var board = []rune(
-
`...........
-
...........
-
....●●●....
-
....●●●....
-
..●●●●●●●..
-
..●●●○●●●..
-
..●●●●●●●..
-
....●●●....
-
....●●●....
-
...........
-
...........
-
`)
-
// center is the position of the center hole if
-
// there is a single one; otherwise it is -1.
-
var center int
-
func init() {
-
n := 0
-
for pos, field := range board {
-
if field == '○' {
-
center = pos
-
n++
-
}
-