golang比较两个以不同方式实现的结构

时间:2018-01-16 22:06:26

标签: go struct

我是新手,学习语言。我有多年的COP OOP经验。有一个堆栈器接口用go和它的两个实现编写,一个是slice基栈,另一个是linkedlist base。

我发现很难比较两种不同的结构,并判断它们是否包含相同的数据。下面的简单示例代码列表(注意很多函数/实现未列出,因为它们与此问题不相关)。关键功能是 stackEquals ,我尝试了不同的方法来解决它,但它们失败了。请参阅代码中的注释。

.train{
    -webkit-animation: progress-bar 5s;
    -moz-animation: progress-bar 5s;
    animation: progress-bar 5s;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-color: #bc2122;
}

@keyframes progress-bar {
  from {
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background: -moz-linear-gradient(bottom, #67A062 0%, #67A062 0%, #bc2122 0%, #bc2122 100%);
    background: -webkit-linear-gradient(bottom, #67A062 0%,#67A062 0%,#bc2122 0%,#bc2122 100%);
    background: linear-gradient(to top, #67A062 0%,#67A062 0%,#bc2122 0%,#bc2122 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr=#67A062, endColorstr=#bc2122,GradientType=0 );
  }
  to {
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background: -moz-linear-gradient(bottom, #67A062 0%, #67A062 60%, #bc2122 60%, #bc2122 100%);
    background: -webkit-linear-gradient(bottom, #67A062 0%,#67A062 60%,#bc2122 60%,#bc2122 100%);
    background: linear-gradient(to top, #67A062 0%,#67A062 60%,#bc2122 60%,#bc2122 100%);
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#67A062, endColorstr=#bc2122,GradientType=0 );
  }
};

如何比较以不同方式实现的两个堆栈,并判断堆栈中它们是否相同(相同的值是相同的值)。

1 个答案:

答案 0 :(得分:2)

如果你需要比较两个接口,你只能使用该接口中的方法,所以在这种情况下,接口中不存在Contributor(即使你的两个实现都有它,接口本身没有)。

可能的实施方式是:

String

这假设唯一的错误func stackEquals(s, t Stacker) bool { // if they are the same object, return true if s == t { return true } // if they have different sizes or the next element is not the same, // then they are different if s.size() != t.size() || s.peek() != t.peek() { return false } // they could be the same, so let's copy them so that we don't mess up // the originals ss = s.copy() tt = t.copy() // iterate through the values and check if each one is // the same. If not, return false for ; i, err := ss.pop(); err == nil { if j, err := tt.pop(); err != nil || i != j { return false } } return true } 会在没有更多值的情况下获得,否则您需要进行更好的错误检查并使用pop

相关问题