Value returns a new Value instance with array element for given index. If index is out of array bounds, Value reports failure and returns empty (but non-nil) instance. Example: array := NewArray(t, []interface{}{"foo", 123}) array.Value(0).String().IsEqual("foo") array.Value(1).Number().IsEqua
(index int)
| 161 | // array.Value(0).String().IsEqual("foo") |
| 162 | // array.Value(1).Number().IsEqual(123) |
| 163 | func (a *Array) Value(index int) *Value { |
| 164 | opChain := a.chain.enter("Value(%d)", index) |
| 165 | defer opChain.leave() |
| 166 | |
| 167 | if opChain.failed() { |
| 168 | return newValue(opChain, nil) |
| 169 | } |
| 170 | |
| 171 | if index < 0 || index >= len(a.value) { |
| 172 | opChain.fail(AssertionFailure{ |
| 173 | Type: AssertInRange, |
| 174 | Actual: &AssertionValue{index}, |
| 175 | Expected: &AssertionValue{AssertionRange{ |
| 176 | Min: 0, |
| 177 | Max: len(a.value) - 1, |
| 178 | }}, |
| 179 | Errors: []error{ |
| 180 | errors.New("expected: valid element index"), |
| 181 | }, |
| 182 | }) |
| 183 | return newValue(opChain, nil) |
| 184 | } |
| 185 | |
| 186 | return newValue(opChain, a.value[index]) |
| 187 | } |
| 188 | |
| 189 | // Deprecated: use Value instead. |
| 190 | func (a *Array) Element(index int) *Value { |