2 回答

TA貢獻(xiàn)1779條經(jīng)驗(yàn) 獲得超6個(gè)贊
所以,如果它是一個(gè)字符串,可能應(yīng)該使用 split 將字符串轉(zhuǎn)換為一個(gè)數(shù)組,那么由于每一行都有相同的列,那么你很可能通過循環(huán)遍歷新創(chuàng)建的數(shù)組來創(chuàng)建一個(gè)結(jié)構(gòu)數(shù)組(創(chuàng)建每一行結(jié)構(gòu)在循環(huán)期間的每 4 次循環(huán)中),那么您將擁有一個(gè)定義明確的結(jié)構(gòu)數(shù)組,您可以查詢和操作。我還沒有把代碼放上去,因?yàn)槲抑皇窍氪_保它實(shí)際上是一個(gè)像下面這樣的字符串?
"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"

TA貢獻(xiàn)1839條經(jīng)驗(yàn) 獲得超15個(gè)贊
我想我會(huì)為上面寫的評(píng)論添加代碼:
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
}
- 2 回答
- 0 關(guān)注
- 160 瀏覽
添加回答
舉報(bào)