funcmain() { arr := make([]int, 0, 10) for i := 0; i < 10; i++ { num := 0 fmt.Scan(&num) arr = append(arr, num) } var h int fmt.Scan(&h) h += 30 count := 0 for _, v := range arr { if v <= h { count++ } } fmt.Println(count) }
funcIsLeap(year int)bool { return year%400 == 0 || (year%4 == 0 && year%100 != 0) } funcmain() { var x, y int fmt.Scan(&x) fmt.Scan(&y) flag := false if x >= 1582 && y <= 3000 && x < y { flag = true } count := 0 arr := make([]int, 0, y-x) for i := x; i <= y; i++ { if IsLeap(i) && flag { count++ arr = append(arr, i) } } fmt.Println(count) for _, v := range arr { fmt.Print(v, " ") } }
funcIsPrime(num int)bool { for i := 2; i*i <= num; i++ { if num%i == 0 { returnfalse } } returntrue } funcmain() { var x int PrimeArr := make([]int, 0, 1000000) for i := 17; i < 1000000; i++ { if IsPrime(i) { PrimeArr = append(PrimeArr, i) } } fmt.Scan(&x) flag := false for _, v := range PrimeArr { if x == v { fmt.Println("YES") flag = true return } } if !flag { fmt.Println("NO") } }
在已经定义全区变量 var DB *gorm.DB 后,如果再 DB,err:=gorm.Open(mysql.Open(path),&gorm.Config{}) 这里看似只声明并定义了新变量err,但实际把DB也给覆盖定义了,也就是说这里的DB不再是全局变量DB,而是再初始化数据库连接中的局部变量DB 正确方法是 var err error DB,err=gorm.Open(mysql.Open(path),&gorm.Config{})
//获取节点信息 //#content > div > div.article > ol > li:nth-child(1) > div > div.info > div.hd > a > span:nth-child(1) docs.Find("#content > div > div.article > ol > li"). Each(func(i int, s *goquery.Selection) { title := s.Find("div > div.info > div.hd > a > span:nth-child(1)").Text() imgTag := s.Find("div > div.pic > a > img") img, ok := imgTag.Attr("src") info := s.Find("div > div.info > div.bd > p:nth-child(1)").Text() rank := s.Find("div > div.info > div.bd > div > span.rating_num").Text() desc := s.Find("div > div.info > div.bd > p.quote > span").Text() if ok { count++ author, actor, time, tags := InfoSplit(info) if title == "" { title = "none" } if img == "" { img = "none" } if author == "" { author = "none" } if actor == "" { actor = "none" } if time == "" { time = "none" } if tags == "" { tags = "none" } if rank == "" { rank = "none" } if desc == "" { desc = "none" } data := Movie{ Title: title, Img: img, Author: author, Actor: actor, Time: time, Tags: tags, Rank: rank, Desc: desc, } InsertDB(&data) fmt.Println(data) } }) } funcInfoSplit(info string) (author, actor, time, tags string) { //电影导演 authorRe, err := regexp.Compile(`导演:.* `) Err(err, "电影导演错误") author = string(authorRe.Find([]byte(info))) author = strings.TrimPrefix(author, "导演:") author = strings.TrimSpace(author)
//电影演员 actorRe, err := regexp.Compile(`主演:(.*)`) Err(err, "电影演员错误") actor = string(actorRe.Find([]byte(info))) actor = strings.TrimPrefix(actor, "主演:") parts := strings.Split(actor, "/") actor = parts[0] actor = strings.TrimSpace(actor)
//电影时间 timeRe, err := regexp.Compile(`(\d+)`) Err(err, "电影时间错误") time = string(timeRe.Find([]byte(info))) time = strings.TrimSpace(time)
type Movie struct { gorm.Model Title string`json:"title" gorm:"type:varchar(255);not null;"` Img string`json:"img" gorm:"type:varchar(256);not null;"` Rank string`json:"rank" gorm:"type:varchar(256);not null;"` Desc string`json:"desc" gorm:"type:varchar(256);not null;"` Tags string`json:"tags"` Author string`json:"author"` Actor string`json:"actor"` Time string`json:"time"` }
funcmain() { InitDB() start := time.Now() for i := 0; i < 10; i++ { num := fmt.Sprintf("%d", i*25) Spider(num) } end := time.Since(start) st := time.Now() ch := make(chanbool) for i := 0; i < 10; i++ { num := fmt.Sprintf("%d", i*25) go Spider2(num, ch) } for i := 0; i < 10; i++ { <-ch } ed := time.Since(st)
//获取节点信息 //#content > div > div.article > ol > li:nth-child(1) > div > div.info > div.hd > a > span:nth-child(1) docs.Find("#content > div > div.article > ol > li"). Each(func(i int, s *goquery.Selection) { title := s.Find("div > div.info > div.hd > a > span:nth-child(1)").Text() imgTag := s.Find("div > div.pic > a > img") img, ok := imgTag.Attr("src") info := s.Find("div > div.info > div.bd > p:nth-child(1)").Text() rank := s.Find("div > div.info > div.bd > div > span.rating_num").Text() desc := s.Find("div > div.info > div.bd > p.quote > span").Text() if ok { count++ author, actor, time, tags := InfoSplit(info) if title == "" { title = "none" } if img == "" { img = "none" } if author == "" { author = "none" } if actor == "" { actor = "none" } if time == "" { time = "none" } if tags == "" { tags = "none" } if rank == "" { rank = "none" } if desc == "" { desc = "none" } data := Movie{ Title: title, Img: img, Author: author, Actor: actor, Time: time, Tags: tags, Rank: rank, Desc: desc, } InsertDB(&data) fmt.Println(data) if ch != nil { ch <- true } } }) } funcInfoSplit(info string) (author, actor, time, tags string) { //电影导演 authorRe, err := regexp.Compile(`导演:.* `) Err(err, "电影导演错误") author = string(authorRe.Find([]byte(info))) author = strings.TrimPrefix(author, "导演:") author = strings.TrimSpace(author)
//电影演员 actorRe, err := regexp.Compile(`主演:(.*)`) Err(err, "电影演员错误") actor = string(actorRe.Find([]byte(info))) actor = strings.TrimPrefix(actor, "主演:") parts := strings.Split(actor, "/") actor = parts[0] actor = strings.TrimSpace(actor)
//电影时间 timeRe, err := regexp.Compile(`(\d+)`) Err(err, "电影时间错误") time = string(timeRe.Find([]byte(info))) time = strings.TrimSpace(time)
funcmain() { // server.Default() creates a Hertz with recovery middleware. // If you need a pure hertz, you can use server.New() h := server.Default(server.WithHostPorts("127.0.0.1:8083")) h.GET("/hello", func(ctx context.Context, c *app.RequestContext) { c.String(consts.StatusOK, "Hello hertz!") })
/* * Copyright 2022 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
funcmain() { // The default listening port is 8888. // You can modify it with server.WithHostPorts(). h := server.Default( server.WithHostPorts("127.0.0.1:8080"), server.WithMaxRequestBodySize(20<<20), server.WithTransport(standard.NewTransporter), )
h.GET("/hello", func(ctx context.Context, c *app.RequestContext) { c.String(consts.StatusOK, "Hello hertz!") })