英文:
String and integer concatenation issues golang
问题
我正在尝试用Go语言编写一个端口扫描器,但由于我对此不太熟悉,所以遇到了一些问题。以下是我目前编写的代码:
package main
import (
"fmt"
"log"
"net"
"os"
)
func main() {
callme()
}
func callme() {
var status string
getip := os.Args[1]
getport := 0
for i := 0; i < 10; i++ {
getport += i
data := getip + ":" + strconv.Itoa(getport)
conn, err := net.Dial("tcp", data)
if err != nil {
log.Println("Connection error:", err)
status = "Unreachable"
} else {
status = strconv.Itoa(getport) + " - " + "Open"
defer conn.Close()
}
fmt.Println(status)
}
}
我从用户那里获取IP作为命令行参数,然后想要扫描该IP上的所有端口。由于net.Dial
函数需要以"ip:port"的格式提供数据,我有点困惑如何每次连接字符串和整数。有人可以帮助我实现这个吗?
英文:
I am trying to write a port scanner in Go, i am facing few problems since i am new to this. Below is the code i have written till now.
package main
import (
"fmt"
"log"
"net"
"os"
)
func main() {
callme()
}
func callme() {
var status string
getip := os.Args[1]
getport := 0
for i := 0; i < 10; i++ {
getport += i
data := getip + ":" + getport
conn, err := net.Dial("tcp", data)
if err != nil {
log.Println("Connection error:", err)
status = "Unreachable"
} else {
status = getport + " - " + "Open"
defer conn.Close()
}
fmt.Println(status)
}
}
I take ip from user as a command line arg, and then want to scan all ports on this ip. Since the net.Dial function needs data in a format like "ip:port" i am kinda confused how to concat string and int each time. Can any1 help me achieve this ?
答案1
得分: 9
一种可能性是使用strconv.Itoa(getport)
将int
转换为string
。另一种可能性是格式化字符串,例如fmt.Sprintf("%s:%d", getip, getport)
或fmt.Sprintf("%d - Open", getport)
。
英文:
One possibility is using strconv.Itoa(getport)
to convert the int
into a string
. Another possibility is formatting the string, as in fmt.Sprintf("%s:%d", getip, getport)
or fmt.Sprintf("%d - Open", getport)
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论