Skip to content

Commit

Permalink
按照review意见修改code
Browse files Browse the repository at this point in the history
  • Loading branch information
dxyinme committed Jan 7, 2024
1 parent f079453 commit 6b2c824
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions mapx/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ func KeysValues[K comparable, V any](m map[K]V) ([]K, []V) {
// 如果keys和values的长度为0,则会返回一个空map
func ToMap[K comparable, V any](keys []K, values []V) (m map[K]V, err error) {
if keys == nil || values == nil {
return nil, fmt.Errorf("keys与values中有至少一个nil")
return nil, fmt.Errorf("keys与values均不可为nil")
}
n := len(keys)
if n != len(values) {
return nil, fmt.Errorf("keys与values长度不相同")
return nil, fmt.Errorf("keys与values的长度不同, len(keys)=%d, len(values)=%d", n, len(values))
}
m = make(map[K]V)
m = make(map[K]V, n)
for i := 0; i < n; i++ {
m[keys[i]] = values[i]
}
Expand Down
8 changes: 4 additions & 4 deletions mapx/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,25 +172,25 @@ func TestToMap(t *testing.T) {
keys: []int{1, 2, 3},
values: []string{"1", "2"},
result: nil,
err: fmt.Errorf("keys与values长度不相同"),
err: fmt.Errorf("keys与values的长度不同, len(keys)=3, len(values)=2"),
},
{
keys: []int{1, 2, 3},
values: nil,
result: nil,
err: fmt.Errorf("keys与values中有至少一个nil"),
err: fmt.Errorf("keys与values均不可为nil"),
},
{
keys: nil,
values: []string{"1", "2"},
result: nil,
err: fmt.Errorf("keys与values中有至少一个nil"),
err: fmt.Errorf("keys与values均不可为nil"),
},
{
keys: nil,
values: nil,
result: nil,
err: fmt.Errorf("keys与values中有至少一个nil"),
err: fmt.Errorf("keys与values均不可为nil"),
},
{
keys: []int{1, 2, 3, 1, 1},
Expand Down
4 changes: 2 additions & 2 deletions tuple/pair/pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ func NewPairs[K any, V any](
values []V,
) ([]Pair[K, V], error) {
if keys == nil || values == nil {
return nil, fmt.Errorf("keys and values should not be nil")
return nil, fmt.Errorf("keys与values均不可为nil")
}
n := len(keys)
if n != len(values) {
return nil, fmt.Errorf("length is different between keys and values, len(keys)=%d, len(values)=%d", n, len(values))
return nil, fmt.Errorf("keys与values的长度不同, len(keys)=%d, len(values)=%d", n, len(values))
}
pairs := make([]Pair[K, V], n)
for i := 0; i < n; i++ {
Expand Down
8 changes: 4 additions & 4 deletions tuple/pair/pair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,25 @@ func (s *testPairSuite) TestNewPairs() {
keys: nil,
values: []string{"1"},
pairs: nil,
err: fmt.Errorf("keys and values should not be nil"),
err: fmt.Errorf("keys与values均不可为nil"),
},
{
keys: []int{1},
values: nil,
pairs: nil,
err: fmt.Errorf("keys and values should not be nil"),
err: fmt.Errorf("keys与values均不可为nil"),
},
{
keys: nil,
values: nil,
pairs: nil,
err: fmt.Errorf("keys and values should not be nil"),
err: fmt.Errorf("keys与values均不可为nil"),
},
{
keys: []int{1, 2},
values: []string{"1"},
pairs: nil,
err: fmt.Errorf("length is different between keys and values, len(keys)=2, len(values)=1"),
err: fmt.Errorf("keys与values的长度不同, len(keys)=2, len(values)=1"),
},
} {
pairs, err := pair.NewPairs(c.keys, c.values)
Expand Down

0 comments on commit 6b2c824

Please sign in to comment.