0

I have a well-formatted byte array that has rows and columns as follows (note that the columns are separated by spaces). The column names and order are guaranteed:

NAME       UUID                                  TYPE      DEVICE 
WAN        6a62d79f-fba2-45b3-a3dd-e847c4706d96  ethernet  ens18  
DMZ        46a55117-b545-407e-af6e-25d48dfe95f5  ethernet  ens21  
LAN        1691607b-9b73-46ff-95c4-9652d062706a  ethernet  ens19  
MGT        a4819491-243c-4e5b-8cef-a49de5a9cb07  ethernet  ens22  
Untrusted  0734a0ea-c242-4333-bece-2b5cb16e3337  ethernet  ens20 

I would like to iterate over each row and populate a structure as follows:

type Device struct {
    connectionID string
    uuid         string
    deviceType   string
    deviceName   string
}

What is the best way to approach this?

5
  • Are the column names/order guaranteed? Commented Jan 9, 2022 at 2:17
  • Perhaps a duplicate to: stackoverflow.com/questions/20768511/… Commented Jan 9, 2022 at 2:18
  • @HymnsForDisco - Yes the order and column names are guaranteed. I have updated the question to add clarity. I also note that it is a byte array, not a string. (a minor detail) Commented Jan 9, 2022 at 2:25
  • @HaochenWu - I don't think this is a duplicate. The byte array is not in CSV format. That go module won't work for this I believe. Commented Jan 9, 2022 at 2:28
  • you can specify the delimiter to be \t instead of , and then it is in CSV format Commented Jan 9, 2022 at 2:38

2 Answers 2

1

So, if it's a string, probably should use split to turn the string into an array, then since each row has the same columns, then you might very well create an array of struct by looping through that newly made array (create each row struct during every 4 times cycle during the loop), then you will have a well defined array of struct that you can query and manipulate. I haven't put the code up yet, because I just want to make sure it's actually a string like following?

"NAME       UUID                                  TYPE      DEVICE 
 WAN        6a62d79f-fba2-45b3-a3dd-e847c4706d96  ethernet  ens18  
 DMZ        46a55117-b545-407e-af6e-25d48dfe95f5  ethernet  ens21  
 LAN        1691607b-9b73-46ff-95c4-9652d062706a  ethernet  ens19  
 MGT        a4819491-243c-4e5b-8cef-a49de5a9cb07  ethernet  ens22  
 Untrusted  0734a0ea-c242-4333-bece-2b5cb16e3337  ethernet  ens20"
Sign up to request clarification or add additional context in comments.

3 Comments

A mix of Split(output,"\n") and Fields() did the trick. Thanks.
To be more accurate: rows := strings.Split(string(out[:]), "\n")
Yes, I was trying to make a map to label each of the column names, then turn that string into an array(removing the first 4 elements as they are headers) then do a loop and use mod 4 with the index to populate the struct, then push it to a array of devices would also do the trick. but I am glad it helped.
0

I thought I would add the code for the comment I wrote above:

type Device struct {
    connectionID   string
    uuid           string
    deviceType     string
    deviceName     string
}

type Devices []Device

// getNetworkConnections retrieves a list of network connections and their associated details.
// Details include: connection name, uuid, type, and device id (aka NIC name).
func getNetworkConnections() Devices {
    nmcliCmd := exec.Command("nmcli", "con", "show")
    cmdOutput, err := nmcliCmd.Output()
    if err != nil {
        log.Fatal(err)
    }

    // Iterate through the devices and populate the type.
    var device Device

    rows := strings.Split(string(cmdOutput[:]), "\n") // Create an array of rows containing the output
    for _, row := range rows[1:] {                    // Skip the heading row and iterate through the remaining rows
        cols := strings.Fields(row) // Extract each column from the row.
        if len(cols) == 4 {
            device.connectionID = cols[0]
            device.uuid = cols[1]
            device.deviceType = cols[2]
            device.deviceName = cols[3]
            devices = append(devices, device)
        }
    }

    return devices
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.