Golang中的缓冲区问题
问题描述:
I'm working with multi-threaded and serialized processes and want to automate my recon process.
My code works like expected, as long as i'm not calling a function called nmap
. When nmap
is called, it exits with the following error:
./recon-s.go:54:12: not enough arguments in call to nmap have () want (chan<- []byte)
This is my code:
package main
import (
"fmt"
"log"
"os/exec"
"sync"
)
var url string
var wg sync.WaitGroup
var ip string
func nikto(outChan chan<- []byte) {
cmd := exec.Command("nikto", "-h", url)
bs, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
outChan <- bs
wg.Done()
}
func whois(outChan chan<- []byte) {
cmd := exec.Command("whois",url)
bs, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
outChan <- bs
wg.Done()
}
func nmap (outChan chan<-[]byte) {
fmt.Printf("Please input IP")
fmt.Scanln(&ip)
cmd := exec.Command("nmap","-sC","-sV","-oA","nmap",ip)
bs,err := cmd.Output()
if err != nil {
log.Fatal(err)
}
outChan <- bs
wg.Done()
}
func main() {
outChan := make(chan []byte)
fmt.Printf("Please input URL")
fmt.Scanln(&url)
wg.Add(1)
go nikto(outChan)
wg.Add(1)
go whois(outChan)
wg.Add(1)
go nmap()
for i := 0; i < 3; i++ {
bs := <-outChan
fmt.Println(string(bs))
}
close(outChan)
wg.Wait()
}
答
The error which one you're getting is:
not enough arguments in call to nmap have () want (chan<- []byte)
It means nmap()
from main
method have not any argument but the actual nmap()
definition want an argument like chan<-[]byte
, So you have to pass an argument from nmap()
like below I mentioned the argument which one you just missed.
func main() {
outChan := make(chan []byte)
fmt.Printf("Please input URL")
fmt.Scanln(&url)
wg.Add(1)
go nikto(outChan)
wg.Add(1)
go whois(outChan)
wg.Add(1)
go nmap(outChan) //you are just missing the argument here.
for i := 0; i < 3; i++ {
bs := <-outChan
fmt.Println(string(bs))
}
close(outChan)
wg.Wait()
}