funcRotate(s []int, position int) []int { r := s[position:] for i := position-1 ; i >= 0; i-- { r = append(r, s[i]) } return r }
练习4.5
编写一个就地处理函数,用于去除[]string slice中相邻的重复字符串元素
设置标记为和当前位置两个参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
package main
funcDeleteRepet(s []string) []string { iflen(s) < 1 { return s } current := s[0] position := 0 for i := 1; i < len(s); i++ { if s[i] != current { position += 1 s[position] = s[i] current = s[i] } } return s[:position+1] }
funcDeleteEmpty(s []rune) []rune { position := 0 tag := unicode.IsSpace(s[0]) for i := 1; i < len(s); i++ { if unicode.IsSpace(s[i]) { if tag != true { position += 1 s[position] = s[i] tag = true } } else { tag = false position += 1 s[position] = s[i] } } return s[:position+1] }
type IssuesSearchResult struct { TotalCount int`json:"total_count"` Items []*Issue }
type Issue struct { Number int HTMLURL string`json:"html_url"` Title string State string User *User CreatedAt time.Time `json:"created_at"` Body string }
type User struct { Login string HTMLURL string`json:"html_url"` }
SortResult(result) var month, year, distant []*Issue
for v, i := range result.Items { if i.CreatedAt.After(time.Now().Add(time.Hour * 24 * 30 * -1)) { month = append(month, result.Items[v]) } elseif i.CreatedAt.After(time.Now().Add(time.Hour * 24 * 365 * -1)) { year = append(year, result.Items[v]) } else { distant = append(distant, result.Items[v]) } }
fmt.Printf("%d issues:\n", result.TotalCount) fmt.Printf("with in a month\n") for _, item := range month { fmt.Printf("#%-5d %9.9s %.55s %s\n", item.Number, item.User.Login, item.Title, item.CreatedAt) } fmt.Printf("with in a year\n") for _, item := range year { fmt.Printf("#%-5d %9.9s %.55s %s\n", item.Number, item.User.Login, item.Title, item.CreatedAt) }
fmt.Printf("long long ago\n") for _, item := range distant { fmt.Printf("#%-5d %9.9s %.55s %s\n", item.Number, item.User.Login, item.Title, item.CreatedAt) } }
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { var data struct { A string B template.HTML } data.A = "<b>Hello!</b>" data.B = "<b>Hello!</b>"