go 语言学习笔记(一)

本次学习目的:熟悉了解 go 语言特点,实现基本功能。

1、readfile.go  

 1 package main
 2 
 3 import (
 4     "bufio"
 5     "fmt"
 6     "io"
 7     "os"
 8 )
 9 
10 func ReadFile(filepath string) []string {
11     fi, err := os.Open(filepath)
12     if err != nil {
13         fmt.Printf("Read file error\n")
14     }
15     defer fi.Close()
16 
17     br := bufio.NewReader(fi)
18     var lineList []string
19     for {
20         line, _, err := br.ReadLine()
21         if err == io.EOF {
22             break
23         }
24         if err != nil {
25             fmt.Println("Error!")
26         }
27         lineList = append(lineList, string(line))
28     }
29     return lineList
30 }

2、main.go 的实现

 1 package main
 2 
 3 import (
 4     "fmt"
 5     "io"
 6     "net/http"
 7     "os"
 8     "path/filepath"
 9     "strings"
10     "time"
11 )
12 
13 var (
14     url      = "http://132.121.00.111:8008/REST/docServiceV2?timestamp=14500903392000&organId=2222&busiType=1&appId=crmlog&staffNo=GZTEST200&filename=JBRZP/20180927/76920180927173700060_713991642.jpg&method=get&password=crmlog1452&latnId=769&signature=731ccc37de5e48dc69bb9ba34b9f3188&version=001"
15     filename = "JBRZP/20180927/76920180927173700060_713991642.jpg"
16     str      = "latnId=769"
17 )
18 
19 func checkErr(err error) {
20     if err != nil {
21         panic(err)
22     }
23 }
24 
25 func CreateDateDir(Path string) string {
26     foldname := time.Now().Format("20190314")
27     foldpath := filepath.Join(Path, foldname)
28     if _, err := os.Stat(foldpath); os.IsNotExist(err) {
29         os.Mkdir(foldpath, 0777)
30         os.Chmod(foldpath, 0777)
31     }
32     return foldpath
33 }
34 
35 func downLoad(buf []string, new_url string, filename string, fold string) {
36     var (
37         i                     int
38         image_url, image_path string
39         list                  []string
40     )
41     for i = 0; i < len(buf); i++ {
42         list = strings.Split(buf[i], "/")
43         image_url = strings.Replace(new_url, filename, buf[i], -1)
44         res, err := http.Get(image_url)
45         image_path = filepath.Join(fold, list[len(list)-1])
46         f, err := os.Create(image_path)
47         checkErr(err)
48         io.Copy(f, res.Body)
49     }
50 }
51 
52 func main() {
53     var (
54         new_url, latnID, newpath string
55     )
56     fmt.Println("请输入区号(如广州:200): ")
57     fmt.Scanln(&latnID)
58     latnID = "latnID=" + latnID
59     path, err := os.Getwd()
60     checkErr(err)
61     newpath = CreateDateDir(path)
62     data_path := path + "\\data.txt"
63     buf := ReadFile(data_path)
64     new_url = strings.Replace(url, str, latnID, -1)
65     downLoad(buf, new_url, filename, newpath)
66     fmt.Println("GetImage Success!")
67 }

 

学习参考:

1、 go 语言学习手册 : https://cloud.tencent.com/developer/doc/1101

2、 go 语言开发 IDE 推荐:LiteIDE

3、 go 开发环境配置参考:https://blog.csdn.net/meiwei_1/article/details/79193853

posted @ 2019-03-15 14:13  那年花开月正圆  阅读(238)  评论(0编辑  收藏  举报