package main

import (
    "fmt"
    "time"
    "sync"
)
 
func main() {
    wg := sync.WaitGroup{}
 
    for i := 0; i < 5; i++ {
        wg.Add(1)
        go func(wg *sync.WaitGroup, i int) {
            fmt.Printf("i:%d\n", i)
            wg.Done()
        }(&wg, i)
    }
 
    wg.Wait()
 
    fmt.Printf("exit\n")
    fmt.Printf("All Goroutine finished!")
}

func main1() {
    var wg sync.WaitGroup

    wg.Add(2) //设置计数器,数值即为goroutine的个数
    go func() {
        //Do some work
        time.Sleep(1*time.Second)

        fmt.Println("Goroutine 1 finished!")
        wg.Done() //goroutine执行结束后将计数器减1
    }()

    go func() {
        //Do some work
        time.Sleep(2*time.Second)

        fmt.Println("Goroutine 2 finished!")
        wg.Done() //goroutine执行结束后将计数器减1
    }()

    wg.Wait() //主goroutine阻塞等待计数器变为0
    fmt.Printf("All Goroutine finished!")
}
emuxevans(Emux Evans) - 2019-09-13 0 人
waitGroup
package main

import (
    "fmt"
    "time"
    "sync"
)
 
func main() {
    wg := sync.WaitGroup{}
 
    for i := 0; i < 5; i++ {
        wg.Add(1)
        go func(wg *sync.WaitGroup, i int) {
            fmt.Printf("i:%d\n", i)
            wg.Done()
        }(&wg, i)
    }
 
    wg.Wait()
 
    fmt.Printf("exit\n")
    fmt.Printf("All Goroutine finished!")
}

func main1() {
    var wg sync.WaitGroup

    wg.Add(2) //设置计数器,数值即为goroutine的个数
    go func() {
        //Do some work
        time.Sleep(1*time.Second)

        fmt.Println("Goroutine 1 finished!")
        wg.Done() //goroutine执行结束后将计数器减1
    }()

    go func() {
        //Do some work
        time.Sleep(2*time.Second)

        fmt.Println("Goroutine 2 finished!")
        wg.Done() //goroutine执行结束后将计数器减1
    }()

    wg.Wait() //主goroutine阻塞等待计数器变为0
    fmt.Printf("All Goroutine finished!")
}
longwalk(前端大神) - 2019-09-03 0 人
waitGroup
package main

import (
	"fmt"
)

func bubbleSort(arrayzor []int) {

	swapped := true;
	for swapped {
		swapped = false
		for i := 0; i < len(arrayzor) - 1; i++ {
			if arrayzor[i + 1] < arrayzor[i] {
				Swap(arrayzor, i, i + 1)
				swapped = true
			}
		}
	}	
}

func Swap(arrayzor []int, i, j int) {
	tmp := arrayzor[j]
	arrayzor[j] = arrayzor[i]
	arrayzor[i] = tmp
}

func main() {

	arrayzor := []int{1, 6, 2, 4, 9, 0, 5, 3, 7, 8}
	fmt.Println("Unsorted array: ", arrayzor)
	bubbleSort(arrayzor)
	fmt.Println("Sorted array: ", arrayzor)
}
2011013333(周二酸) - 2019-09-02 0 人
冒泡排序实现
package main
import "fmt"
func main () {
    var ch chan int      // 声明一个传递int类型的channel

    //=========
    value := 5
    ch <- value          // 将一个数据value写入至channel,这会导致阻塞,直到有其他goroutine从这个channel中读取数据
    value1 := <-ch        // 从channel中读取数据,如果channel之前没有写入数据,也会导致阻塞,直到channel中被写入数据为止
    fmt.Println(value1)

    close(ch)            // 关闭channel

    fmt.Println("Hello JSRUN! \n")
}
longwalk(前端大神) - 2019-08-27 1 人
test1.go
package main
import "fmt"

type Point struct{ X, Y float64 }
func main () {
    //程序运行完成时一定要有输出语句,本工具才能正确展示运行结果。
    a, b, c := true, 2, "hello"
    fmt.Printf("%t %d %s\n", a, b, c)
    var x, y int
    fmt.Println(&x == &x, &x == &y, &x == nil) // "true false false"
    

    
    r := Point{1, 2}
    r.ScaleBy(2)
    fmt.Println(r) // "{2, 4}"
    

    fmt.Println("Hello\n")
}

func changeArray(array [3]int)(newAraay [3]int){
    array[0]=8
    return array
}

func (p *Point) ScaleBy(factor float64) {
    p.X *= factor
    p.Y *= factor
}
longwalk(前端大神) - 2019-08-27 0 人
test.go
package main

import "fmt"
import "math/rand"


func quick_sort(arr []int) []int {
    
    if len(arr) <= 1 {
        return arr
    }
 
    median := arr[rand.Intn(len(arr))]
    
    low_part := make([]int, 0, len(arr))
    high_part := make([]int, 0, len(arr))
    middle_part := make([]int, 0, len(arr))
 
    for _, item := range arr {
        switch {
        case item < median:
            low_part = append(low_part, item)
        case item == median:
            middle_part = append(middle_part, item)
        case item > median:
            high_part = append(high_part, item)
        }
    }
 
    low_part = quick_sort(low_part)
    high_part = quick_sort(high_part)

    low_part = append(low_part, middle_part...)
    low_part = append(low_part, high_part...)
 
    return low_part
}

func main() {
    arr := RandomArray(10)

    fmt.Println("Initial array is:", arr)
    fmt.Println("")
    fmt.Println("Sorted array is: ", quick_sort(arr))
}

func RandomArray(n int) []int {
    arr := make([]int, n)
    for i := 0; i <= n - 1; i++ {
        arr[i] = rand.Intn(n)
    }
    return arr
}
2011013333(周二酸) - 2019-08-09 0 人
快速排序-go
没有了
1/5 下一页