英文:
How to find overlapping values in list of objects in golang?
问题
package main
import (
"fmt"
)
type Column struct {
Name string `json:"name"`
Type string `json:"type"`
}
func main() {
a := []*Column{
{Name: "a", Type: "int"},
{Name: "b", Type: "int"},
}
b := []*Column{
{Name: "a", Type: "int"},
{Name: "c", Type: "string"},
}
c := []*Column{
{Name: "a", Type: "string"},
{Name: "d", Type: "int"},
}
}
// 需要找出在比较两个对象列表时,是否存在具有不同类型的重叠名称,如果没有则返回false。有没有关于优化逻辑的建议?
func check(obj1, obj2 []*Column) bool {
for _, col1 := range obj1 {
for _, col2 := range obj2 {
if col1.Name == col2.Name && col1.Type != col2.Type {
return true
}
}
}
return false
}
以上是给定的代码,其中定义了一个Column
结构体,包含Name
和Type
两个字段。在main
函数中,创建了三个对象列表a
、b
和c
。
check
函数用于比较两个对象列表obj1
和obj2
,并判断是否存在具有不同类型的重叠名称。函数通过嵌套的循环遍历两个列表中的每个对象,如果找到具有相同名称但类型不同的对象,则返回true
,否则返回false
。
希望以上信息对你有帮助!如果有任何其他问题,请随时提问。
英文:
package main
import (
"fmt"
)
type Column struct {
Name string `json:"name"`
Type string `json:"type"`
}
func main() {
a := []*Column{
{Name: "a", Type: "int"},
{Name: "b", Type: "int"},
}
b := []*Column{
{Name: "a", Type: "int"},
{Name: "c", Type: "string"},
}
c := []*Column{
{Name: "a", Type: "string"},
{Name: "d", Type: "int"},
}
}
Need to find if there is any overlapping Name with different Type when comparing 2 list of objects, if not return false. Any suggestions for optimized logic?
func check(obj1,obj2){
// when comparing a and b it would return false as both Name="a" has identical Type="int"
// when comparing b and c it would return true as both Name="a" have different Types
}
答案1
得分: 0
你可以使用map来存储和比较键:
package main
import (
"fmt"
)
type Column struct {
Name string `json:"name"`
Type string `json:"type"`
}
func check(c1, c2 []*Column) bool {
m := make(map[string]string)
for _, col := range c1 {
m[col.Name] = col.Type
}
for _, col := range c2 {
if t, found := m[col.Name]; found && t != col.Type {
return true
}
}
return false
}
func main() {
a := []*Column{
{Name: "a", Type: "int"},
{Name: "b", Type: "int"},
}
b := []*Column{
{Name: "a", Type: "int"},
{Name: "c", Type: "string"},
}
c := []*Column{
{Name: "a", Type: "string"},
{Name: "d", Type: "int"},
}
fmt.Println(check(a, b)) //false
fmt.Println(check(a, c)) //true
fmt.Println(check(b, c)) //true
}
在Go playground上测试它。
英文:
You can use a map to store and compare the keys:
package main
import (
"fmt"
)
type Column struct {
Name string `json:"name"`
Type string `json:"type"`
}
func check(c1, c2 []*Column) bool {
m := make(map[string]string)
for _, col := range c1 {
m[col.Name] = col.Type
}
for _, col := range c2 {
if t, found := m[col.Name]; found && t != col.Type {
return true
}
}
return false
}
func main() {
a := []*Column{
{Name: "a", Type: "int"},
{Name: "b", Type: "int"},
}
b := []*Column{
{Name: "a", Type: "int"},
{Name: "c", Type: "string"},
}
c := []*Column{
{Name: "a", Type: "string"},
{Name: "d", Type: "int"},
}
fmt.Println(check(a, b)) //false
fmt.Println(check(a, c)) //true
fmt.Println(check(b, c)) //true
}
Test it on Go playground
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论