英文:
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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论