package main web
import (
"bufio"
"fmt"
"io"
"os"
"strings"
)
var UrlMap = make(map[string]int)
func main() {
inputFile, err := os.Open("ex14060609.log")
if err != nil {
fmt.Println(err)
}
defer inputFile.Close()
bufRD := bufio.NewReader(inputFile)
for {
str, err := bufRD.ReadString('\n')
if err == io.EOF {
break
}
//以"#"開頭的要跳過,iiS 日誌前幾行是作了註釋的
if strings.HasPrefix(str, "#") {
continue
}
//fmt.Printf("%s", str)
//tmpArr 定義臨時數組
tmpArr := strings.Split(str, " ")
//fmt.Println(tmpArr[5])
//經過日誌文件可知temArr[5]在web日誌裏是URL
//str1 = tmpArr[5]
//var temp UrlItem
//sw變量的做用是一個開關,用它來判斷tmpArr[5]是否已經在map裏,若是不在,就新建一個map鍵值對
sw := true
for k, _ := range UrlMap {
if k == tmpArr[5] {
UrlMap[k]++
sw = false
break
}
}
if sw {
UrlMap[tmpArr[5]] = 1
}
}
for k, v := range UrlMap {
fmt.Printf("%s : %d\n", k, v)
}
//fmt.Printf("ddd")
//UrlMap[tmpArr[5]] = 1
}