Home > other >  Parsing structured data in golang
Parsing structured data in golang

Time:01-09

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?

CodePudding user response:

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"
  •  Tags:  
  • Related