第一节:go语言入门

go语言入门

demo:

package main

import "fmt"

func main() {
    fmt.Printf("hello, world\n")
}

run:

go run helloworld.go

生成二进制文件:

go build helloworld.go

包:

一个或者多个源文件构成了包

读取命令行:

package main

import "fmt"
import "os"

func main() {
    fmt.Println("hello zhang lei");
    var s,sep string;
    for i := 1;i<len(os.Args);i++{
        s += sep + os.Args[i];

    }
    fmt.Print(s+"\n");

}

:=号是一个短变量声明

for是唯一的循环:

for init;condition;post

//类似与import
package main

import (
    "fmt"
    "strings"
)
import "os"

func main() {
    fmt.Println(strings.Join(os.Args[1:]," "))

}

//如果说缺少[1:]就代表从第一个一直到最后一个

1.3找出重复行

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main(){
    counts := make(map[string]int)

    input := bufio.NewScanner(os.Stdin)

    for input.Scan(){
        counts[input.Text()]++
        fmt.Printf("%s\n",input.Text());

    }

    for line,n := range counts{
        if n>1 {
            fmt.Printf("%d\t%s\n",n,line);
        }
    }
}


下一个demo

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    content := make(map[string]int)
    files := os.Args[1:] //获取命令行参数里的文件路径
    if len(files) == 0 {
        //从标准输入中读取数据
        countLines(os.Stdin, content)
    } else {
        //从文件中读取数据
        //如果有多个文件
        for _, path := range files {
            f, err := os.Open(path) //读取文件
            //读取出错
            if err != nil {
                fmt.Fprintf(os.Stderr, "dup2:%v \n", err)
                continue
            }
            countLines(f, content)
            //注意方法名的大小写
            f.Close()
        }
    }
    for line, n := range content {
        if n > 1 {
            fmt.Printf("%d\t%s\n", n, line)
        }
    }
}

//声明一个函数,实参类型:*os.File,content map[string]int
func countLines(f *os.File, content map[string]int) {
    input := bufio.NewScanner(f)
    for input.Scan() {
        content[input.Text()]++
    }
}

demo2:

package main

import (
    "fmt"
    "os"
    "io/ioutil"
    "strings"
)

func main()  {
    counts := make(map[string]int)

    for _,filename := range os.Args[1:]{
        data,err := ioutil.ReadFile(filename)
        if err != nil{
            fmt.Fprintf(os.Stderr,"dup3:%v \n",err);
            continue;
        }
        for _,line := range strings.Split(string(data),"\n"){
            fmt.Printf("count:%d\n",counts[line])
            counts[line]++
        }
    }
    for line,n:=range counts{
        if n>1{
            fmt.Printf("%d\tstring:%s\n",n,line)
        }
    }
}

利萨如曲线:

package main

import (
    "image"
    "image/color"
    "image/gif"
    "io"
    "log"
    "math"
    "math/rand"
    "net/http"
    "os"
    "time"
)

var palette = []color.Color{color.White,color.Black}

const (
    whiteIndex = 0
    blackIndex = 1
)

func main()  {
    rand.Seed(time.Now().UTC().UnixNano())

    if len(os.Args) > 1 && os.Args[1] == "web"{
        handler := func(w http.ResponseWriter,r *http.Request) {
            lissajous(w)
        }

        http.HandleFunc("/",handler)
        log.Fatal(http.ListenAndServe("localhost:8000",nil))
        return
    }

    lissajous(os.Stdout)
}

func lissajous(out io.Writer) {
    const (
        cycles = 5
        res = 0.001
        size = 100
        nframes = 64
        delay = 8
    )

    freq := rand.Float64() * 3.0

    anim := gif.GIF{LoopCount:nframes}

    phase := 0.0

    for i:=0;i<nframes;i++{
        rect := image.Rect(0,0,2*size+1,2*size+1)
        img := image.NewPaletted(rect,palette)

        for t:=0.0;t<cycles*2*math.Pi;t+=res{
            x:= math.Sin(t)
            y:=math.Sin(t*freq+phase)
            img.SetColorIndex(size+int(x*size+0.5),size+int(y*size+0.5),blackIndex)
        }

        phase += 0.1
        anim.Delay = append(anim.Delay,delay)
        anim.Image = append(anim.Image,img)
    }

    gif.EncodeAll(out,&anim)
}

获取一个url的内容:

package main

import(
    "fmt"
    "io/ioutil"
    "net/http"
    "os"
)

func main(){
    for _,url := range os.Args[1:]{
        resp,err := http.Get(url)

        if err != nil{
            fmt.Fprintf(os.Stderr,"fetch:%v\n",err)
        }

        b,err := ioutil.ReadAll(resp.Body)

        resp.Body.Close()

        if err != nil{
            fmt.Fprintf(os.Stderr,"fetch:reading %s:%v\n",url,err)
            os.Exit(1)
        }

        fmt.Printf("%s",b)
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值