如何使用Go语言以结构化的方式高效解析字符串?

huangapple go评论71阅读模式
英文:

How to parse string effectively in a structured manner using Go?

问题

我有两个带有键和值的映射,如下所示。这里的值是一个字符串,我该如何有效地比较每个网关的内容(值),并确定它们不相同?有没有一种结构化的方法来实现这个?

键:Gateway1

值:

 | 属性                           | 值                   |
 | ----------------------------- | --------------------|
 | ADDRESS                       |   ipv4:10.1.1.1     |
 | CERT_EXPIRY_LEAD_TIME         |   8                 |
 | CLIENT_CERT_REQ               |   TRUE              |
 | PORT                          |   5000              |
 | SUPPORT_TLS                   |   FALSE             |
 | TLS_DTLS_VERSION              |   1.1               |
 |  TRANSPORT                    |   TCP               |

键:Gateway2

值:

 | 属性                           | 值                   |
 | ----------------------------- | --------------------|
 | ADDRESS                       |   ipv4:10.1.1.2     |
 | CERT_EXPIRY_LEAD_TIME         |   8                 |
 | CLIENT_CERT_REQ               |   TRUE              |
 | PORT                          |   4000              |
 | SUPPORT_TLS                   |   FALSE             |
 | TLS_DTLS_VERSION              |   1.2               |
 |  TRANSPORT                    |   TCP               |

请注意,我只是将您提供的内容翻译成中文,并没有回答您的问题。

英文:

I have two maps with a key & value like the below.
Value is a string here, how can I effectively compare the content(value) of each of these gateways and identify that both are not same? Any structured way to achieve this?

> Key: Gateway1

Value:

 | Attribute                     | Value               |
 | ----------------------------- | --------------------|
 | ADDRESS                       |   ipv4:10.1.1.1     |
 | CERT_EXPIRY_LEAD_TIME         |   8                 |
 | CLIENT_CERT_REQ               |   TRUE              |
 | PORT                          |   5000              |
 | SUPPORT_TLS                   |   FALSE             |
 | TLS_DTLS_VERSION              |   1.1               |
 |  TRANSPORT                    |   TCP               |

> Key: Gateway2

Value:

 | Attribute                      |Value                    |
 | -----------------------------  |-------------------------|
 | ADDRESS                        |  ipv4:10.1.1.2          |
 | CERT_EXPIRY_LEAD_TIME          |  8                      |
 | CLIENT_CERT_REQ                |  TRUE                   |
 | PORT                           |  4000                   |
 | SUPPORT_TLS                    |  FALSE                  |
 | TLS_DTLS_VERSION               |  1.2                    |
 | TRANSPORT                      |  TCP                    |

答案1

得分: 1

你可以使用DeepEqual来比较地图。

以下是一个示例代码(基本上是从这里复制过来的):

/* 相等 */

func main() { 
    map_1 := map[int]string{   
        1: "One",
        2: "Two",
    }

    map_2 := map[int]string {   
        1: "One",
        2: "Two",
    }     
    res1 := reflect.DeepEqual(map_1, map_2)
    fmt.Println("相等: ", res1)
}

/* 不相等 */

func main() { 
    map_1 := map[int]string{   
        3: "Three",
        4: "Four",
    }

    map_2 := map[int]string {   
        1: "One",
        2: "Two",
    }     
    res1 := reflect.DeepEqual(map_1, map_2)
    fmt.Println("不相等: ", res1)
}
英文:

For comparing maps you could use DeepEqual.

Here is a sample code (basically taken from here)

/* equal */

func main() { 
    map_1 := map[int]string{   
        1: "One",
        2: "Two",
    }

    map_2 := map[int]string {   
        1: "One",
        2: "Two",
    }     
    res1 := reflect.DeepEqual(map_1, map_2)
    fmt.Println("equal ", res1)
}

/* NOT equal */

func main() { 
    map_1 := map[int]string{   
        3: "Three",
        4: "Four",
    }

    map_2 := map[int]string {   
        1: "One",
        2: "Two",
    }     
    res1 := reflect.DeepEqual(map_1, map_2)
    fmt.Println("Not equal: ", res1)
}

huangapple
  • 本文由 发表于 2022年8月19日 13:04:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/73412039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定