Re: [go-nuts] Struct field updefined Build Error

2016-11-25 Thread Jan Mercl
On Fri, Nov 25, 2016 at 4:42 PM Chris S  wrote:

type AggInfoXml struct {
Percent IntPercent `xml:"taskprogress,omitempty"`
IpAddr []Addr `xml:"host>address,omitempty"`
}

...

for _, port := range aggInfoXml.IpAddr.Hports {
fmt.Printf("Port: %s\n", port.ports())
}

aggInfoXml.IpAddr is of type []Addr, ie it's a slice. Slices do not have
fields, structs have. You probably forgot to use an index:

for _, port := range aggInfoXml.IpAddr[someIndexExresion].Hports {
fmt.Printf("Port: %s\n", port.ports())
}


-- 

-j

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Struct field updefined Build Error

2016-11-25 Thread Chris S
Hi,

I am currently working on parsing an xml file using go structs. I have 
typed up the following code below. But for some reason when I try to 
compile it. it gives me an error saying 

./main_v4.go:155: aggInfoXml.IpAddr.Hports undefined (type []Addr has no 
field or method Hports)

I have no idea what is causing this issue. Any thoughts would be helpeful.

package main

import (

"net/http"
"html/template"
"os/exec"
"io/ioutil"
"os"
"encoding/xml"
"encoding/json"
"fmt"
"bufio"
"github.com/gorilla/websocket"
"time"
"log"

)

type PercentInfo struct {
Percent IntPercent `xml:"taskprogress,omitempty"`
} 

type AggInfoXml struct {
Percent IntPercent  `xml:"taskprogress,omitempty"`
IpAddr  []Addr  `xml:"host>address,omitempty"`
}

type IntPercent struct {
Value float64 `xml:"percent,attr,omitempty"` // works
}


type Addr struct {
Ip  string   `xml:"addr,attr"`
Hports  []Ports  `xml:"host>ports>port,omitempty"` //failing to be 
recognized
}

type Ports struct {
Port string `xml:"portid,attr"`
}

func (p *Addr) ipaddr() string {
 return p.Ip
}

func (p *Ports) ports() string {
 return p.Port
}

func (p *IntPercent) percent() float64 {
return p.Value
}

func (f *Test) Name() string {
return f.Value

}

var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}

type Test struct {
Value string
}


func wsHandler(w http.ResponseWriter, r *http.Request) {

conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println(err)
}

cmd := exec.Command("nmap", "-F", "-sS", "172.16.2.0/24", "-oX", 
"output.xml", "--stats-every", "1s")

if err := cmd.Start(); err != nil {
log.Fatal(err)
}

time.Sleep( 1000 * time.Millisecond) 

fp, err := os.Open("output.xml")
if err != nil {
log.Fatal(err)
}


// create scanner on the output.xml file
in := bufio.NewScanner(fp)

aggInfoXml  := AggInfoXml{}


fmt.Printf("hello")

for in.Scan() {

err = xml.Unmarshal([]byte(in.Text()), ) // this 
is not working..
if err != nil {
fmt.Printf("Could not unmarshal the line <%v>", in.Text())
} else {
fmt.Printf("%+v\n", aggInfoXml.Percent.Value) // this is not 
working? why .percent()
}

// bail out of slow loop if 100% reached
if aggInfoXml.Percent.Value == 100 {
break
}

time.Sleep(1000*time.Millisecond)

bytesArray, err := json.Marshal(aggInfoXml.Percent)

err = conn.WriteMessage(websocket.TextMessage, bytesArray)  // send 
JSON over
if err != nil {
panic(err)
}

}//this is the end of the in.Scan loop now

fmt.Println("After loop")


if err := cmd.Wait(); err != nil {  
log.Fatal(err)
}

data, err := ioutil.ReadFile("output.xml")

if err != nil {
panic(err)
}

err = xml.Unmarshal(data, )
if err != nil {
log.Fatal("Could not unmarshal host")
}

for _, port := range aggInfoXml.IpAddr.Hports {
fmt.Printf("Port: %s\n", port.ports())
}

for _, ip := range aggInfoXml.IpAddr {
fmt.Printf("IpAdd: %s\n", ip.ipaddr())
}

conn.Close()
fp.Close()
}

func handler(w http.ResponseWriter, r *http.Request) {

t, _ := template.ParseFiles("index.html")
t.Execute(w,nil)
}

func main() {

http.HandleFunc("/", handler)
http.Handle("/layout/", http.StripPrefix("/layout/", http.FileServer(
http.Dir("layout"
http.HandleFunc("/websocket", wsHandler)
http.ListenAndServe(":8080", nil)

}


-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.