编辑代码

package main
import ("fmt";
        "strconv";
        "os";
        "log"
        "time"
        "net/http"
        "io")
const PI = 3.14
type Cal_Area interface{
    calArea()
}

type Rectangle struct
{
    width float32
    len float32
}

func (p Rectangle)calArea(){
    fmt.Println("The area of Rectangle is:",(p.width)*(p.len))
}

type Circle struct
{
    radius float32
}

func (p Circle)calArea(){
    fmt.Println("The area of Circle is:",PI*(p.radius)*(p.radius))
}

func test_interface () {
    //JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。
    fmt.Println("Hello world!   -  go.jsrun.net ")
    var cal Cal_Area
    cal = Rectangle{width:10.0,len:20.0}
    cal.calArea()

    cal = Circle{radius:10.0}
    cal.calArea()
}

func test_for(){
    userMap := make(map[string]string)
    user := "user"
    for i:=1;i<10;i+=1{
        tmpuser := user + strconv.Itoa(i)
        userMap[tmpuser]=strconv.Itoa(i)
    }
    fmt.Println("test for map user2",userMap["user2"])

    // 遍历输出顺序与插入顺序不同
    for usr,pass := range userMap{
        fmt.Printf("%s\t %s\n",usr,pass)
    }
}

type error interface{
    Error() string
}

func readFilePanic(path string){
    op,err := os.Open(path)
    if err!=nil{
        panic(err)
    }
    // fmt.Println("file name:",op.name)
    fmt.Println("file name:",op.Name())
}

func test_error(path string){
    file, err := os.Open(path)
    if err != nil{
        log.Fatal(err)
    }
    fmt.Println(file.Name())
}

func worker(count int){
    for i:=1;i<count;i++{
        time.Sleep(500)
        fmt.Print(" ",i)
    }
    fmt.Println()
}

func test_goroutine(){
    //不使用协程
    now1 := time.Now()
    for i:=0;i<3;i++{
        worker(10)
    }
    now2 := time.Now()
    fmt.Println("不开协程花费时间:",now2.Sub(now1))

    //使用协程
    for i:=0;i<3;i++{
        go worker(10)
    }
    now3 := time.Now()
    fmt.Println("开协程花费时间:",now3.Sub(now2))
}   

//channel 在不同goroutine之间传递数据  同步执行
//多个goroutine之间安全高效通信
func sum_no_chan(s []int,result *int){
    *result = 0
    for _, v := range s{
        *result += v
    }
}

func sum_chan(s []int,c chan int){
sum := 0
    for _,v := range s{
        sum += v
    }
    c <- sum
}

//channel 测试 利用多个线程同时计算数组的前半部分  和后半部分
func test_channel(){
    s := []int{1,2,3,4,5,6,7,8}
    // 不使用 管道
    now1 := time.Now()
    sum_1 := 0
    sum_no_chan(s,&sum_1)
    now2 := time.Now()
    // fmt.Println("no channel result:%d \t time: %s",sum_1,now2.Sub(now1))
    fmt.Printf("no channel result:%d \t time: %s",sum_1,now2.Sub(now1))
    
    // 使用 管道
}

// test http
func test_http(){
    resp,err := http.Get("http://127.0.0.1")
    if err != nil{
        log.Fatal(err)
    }
    body,err := io.ReadAll(resp.Body)
    if err != nil{
        return 
    }
    fmt.Printf("status %d\n",resp.StatusCode)
    // fmt.Printf("长度 %d\n",resp.ContentLength)
    fmt.Printf("content %d\n",string(body))
}

func main (){
    // test_interface()
    // test_for()
    // test_error("/etc/")
    // test_goroutine()
    // test_channel()
    test_http()
}