在 golang 中迭代多个返回值

我正在尝试从包含域(未知数量)的文本文件中获取输入,然后将每个域用作参数并获取其服务器类型。正如预期的那样,这只会返回最后一个域。如何迭代多个返回值?下面是代码。// 测试包主


import (

    "bufio"

    "time"

    "os"

    "fmt"

    "net/http"

    //"github.com/gocolly/colly"

)


var Domain string

var Target string


func main() {

    Domain := DomainGrab()

    Target := BannerGrab(Domain)

    //CheckDB if not listed then add else skip

    //RiskDB

    //Email

    fmt.Println(Domain)

    fmt.Println(Target)

}


func BannerGrab(s string) string {


    client := &http.Client{}

    req, err := http.NewRequest("GET", s, nil)

    if err != nil {

    log.Fatalln(err)

    }

    req.Header.Set("User-Agent", "Authac/0.1")

    resp, _ := client.Do(req)

    serverEntry := resp.Header.Get("Server")

    return serverEntry


}

func DomainGrab() string {


    //c := colly.NewCollector()

// Open the file.

    f, _ := os.Open("domains.txt")

    defer f.Close()

    // Create a new Scanner for the file.

    scanner := bufio.NewScanner(f)

    // Loop over all lines in the file and print them.

    for scanner.Scan() {

        line := scanner.Text()

        time.Sleep(2 * time.Second)

        //fmt.Println(line)

        return line

    }

    return Domain

}


跃然一笑
浏览 115回答 3
3回答

肥皂起泡泡

如果我理解你的问题,你想读取文件,以某种方式检测到该文件被修改,并有一个方法将这些修改发送到客户端代码。这不是文件的工作方式。你有两个选择:使用一些操作系统特定的 API 监听文件更改 使用无限循环读取文件。读取文件一次。将副本保存到内存中。在循环中一次又一次地读取同一个文件,直到新文件与副本不同并计算增量。检查是否可以使用它push来代替pull获取新域。难不成控制文件域名的系统会直接给你推送数据?如果loop是唯一可能的选项,请在文件读取之间设置一些暂停时间以减少系统负载。

RISEBY

如果您想“同时”执行此操作,您将返回一个通道,您将通过该通道发送您想要返回的多个内容:https://play.golang.org/p/iYBGPwfYLYRfunc DomainGrab() <-chan string {&nbsp; &nbsp; ch := make(chan string, 1)&nbsp; &nbsp; f, _ := os.Open("domains.txt")&nbsp; &nbsp; defer f.Close()&nbsp; &nbsp; scanner := bufio.NewScanner(f)&nbsp; &nbsp; go func() {&nbsp; &nbsp; // Loop over all lines in the file and print them.&nbsp; &nbsp; &nbsp; &nbsp; for scanner.Scan() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; line := scanner.Text()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time.Sleep(2 * time.Second)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ch <- line&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; close(ch)&nbsp; &nbsp; }()&nbsp; &nbsp; return ch}

隔江千里

可能不是最好的解决方案。但是,我决定一起摆脱一个单独的功能,以覆盖更多领域。我会在下面发布我期望的代码。现在,我需要解析域,以便只扫描一次根 URL 和子域。// Mainpackage mainimport (&nbsp; &nbsp; "log"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "time"&nbsp; &nbsp; "net/http"&nbsp; &nbsp; "github.com/gocolly/colly")//var Domain stringvar Target stringfunc main() {&nbsp; &nbsp; c := colly.NewCollector()&nbsp; &nbsp; c.OnError(func(r *colly.Response, err error) {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Request URL:", r.Request.URL, "\n Failed with response:", r.StatusCode)&nbsp; &nbsp; })&nbsp; &nbsp; // Find and visit all links&nbsp; &nbsp; c.OnHTML("a", func(e *colly.HTMLElement) {&nbsp; &nbsp; &nbsp; &nbsp; e.Request.Visit(e.Attr("href"))&nbsp; &nbsp; })&nbsp; &nbsp; c.OnRequest(func(r *colly.Request) {&nbsp; &nbsp; &nbsp; &nbsp; Domain := r.URL.String()&nbsp; &nbsp; &nbsp; &nbsp; Target := BannerGrab(Domain)&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(Domain)&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(Target)&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Dropping By.. ", r.URL)&nbsp; &nbsp; &nbsp; &nbsp; time.Sleep(1000 * time.Millisecond)&nbsp; &nbsp; })&nbsp; &nbsp; c.Visit("http://www.milliondollarhomepage.com/")&nbsp; &nbsp; }&nbsp; &nbsp; //CheckDB if not listed else add&nbsp; &nbsp; //RiskDB&nbsp; &nbsp; //Emailfunc BannerGrab(s string) string {&nbsp; &nbsp; client := &http.Client{}&nbsp; &nbsp; req, err := http.NewRequest("GET", s, nil)&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatalln(err)&nbsp; &nbsp; }&nbsp; &nbsp; req.Header.Set("User-Agent", "Authac/0.1")&nbsp; &nbsp; resp, _ := client.Do(req)&nbsp; &nbsp; serverEntry := resp.Header.Get("Server")&nbsp; &nbsp; return serverEntry}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go