简体   繁体   中英

Cannot use data (type []string) as type string in argument to function

I am getting an error message while saving the data into file and the data I am fetching from internet unmarshelling and when I am trying to save the same data into a text file it is generating an error message ie

"./main.go:24:11: cannot use data (type []string) as type string in argument to fileWrite"

Below is my file

main.go

package main

import (
    "bufio"
    "fmt"
    "log"
    "os"

    crt "github.com/Sectestlist/Subdomainscanner/Modules"
)

func main() {

    scanner := bufio.NewScanner(os.Stdin)
    fmt.Print("Enter New Domain: ")

    // Scans a line from Stdin(Console)
    scanner.Scan()

    // Holds the string that scanned
    domain := scanner.Text()
    data := crt.Cert(domain)
    fmt.Println(data)
    fileWrite(data) // Error ./main.go:24:11: cannot use data (type []string) as type string in argument to fileWrite

}

func fileWrite(y string) {

    reader := bufio.NewReader(os.Stdin)
    fmt.Print("Enter File Name: ")
    text, _ := reader.ReadString('\n')
    file, err := os.OpenFile(text, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)

    if err != nil {
        log.Fatalf("failed creating file: %s", err)
    }

    datawriter := bufio.NewWriter(file)
    datawriter.WriteString(y + "\n")

    datawriter.Flush()
    file.Close()
}

and than there is another file inside package modules cert.go

cert.go

package crt

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
)

type CTLog struct {
    IssuerCaID        int    `json:"issuer_ca_id"`
    IssuerName        string `json:"issuer_name"`
    NameValue         string `json:"name_value"`
    MinCertID         int    `json:"min_cert_id"`
    MinEntryTimestamp string `json:"min_entry_timestamp"`
    NotBefore         string `json:"not_before"`
    NotAfter          string `json:"not_after"`
}

func Cert(domain string) []string {

    out := make([]string, 0)
    url := fmt.Sprintf("https://crt.sh/?output=json&CN=%s", domain)

    resp, err := http.Get(url)

    if err != nil {
        fmt.Printf("The Http request failed %s", err)
    }

    data, err2 := ioutil.ReadAll(resp.Body)
    if err2 != nil {
        fmt.Printf("The request read data failed %s", err)
    }

    bytes := []byte(data)
    var responseObject []CTLog

    json.Unmarshal(bytes, &responseObject)

    for _, x := range responseObject {
        out = append(out, x.NameValue)

    }
    return out
}

Write map string as string separated by new line \n . Added return after error occurence. Close file should be defered after open, also fixed.

func fileWrite(lines []string) {
    reader := bufio.NewReader(os.Stdin)
    fmt.Print("Enter File Name: ")
    text, _ := reader.ReadString('\n')

    file, err := os.OpenFile(text, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
    if err != nil {
        log.Fatalf("failed creating file: %s", err)
        return
    }
    defer file.Close()

    datawriter := bufio.NewWriter(file)
    _, err := datawriter.WriteString(strings.Join(lines, "\n"))
    if err != nil {
        log.Fatalf("failed writing to file: %s", err)
        return
    }

    err = datawriter.Flush()
    if err != nil {
        log.Fatalf("failed to flush the writer: %s", err)
        return
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM