分页切片

huangapple go评论91阅读模式
英文:

Paginating a slice

问题

在Go语言中,遍历切片并避免出现"panic: runtime error: slice bounds out of range"错误的最佳方法是什么?

例如,如果我有一个切片:

slice := []int{1, 2, 3, 4, 5, 6, 7}

并且我调用:

slice[6:10]

我会得到一个panic错误。

我想到了以下方法:

func paginate(x []int, skip int, size int) []int {
  limit := func() int {
    if skip+size > len(x) {
      return len(x)
    } else {
      return skip + size
    }
  }

  start := func() int {
    if skip > len(x) {
      return len(x)
    } else {
      return skip
    }
  }
  return x[start():limit()]
}

playground链接

在Go语言中是否有更好的方法来实现这个功能?

谢谢。

英文:

What's the best way to paginate over a slice in Go and avoid errors like panic: runtime error: slice bounds out of range

For example, if I have a slice like

slice := []int{1, 2, 3, 4, 5, 6, 7}

and I call

slice[6:10]

I get a panic error.

I came up with this:

func paginate(x []int, skip int, size int) []int {
  limit := func() int {
    if skip+size > len(x) {
      return len(x)
    } else {
      return skip + size
    }

  }

  start := func() int {
    if skip > len(x) {
      return len(x)
    } else {
      return skip
    }

  }
  return x[start():limit()]
}

link to playground

Are there better ways to do this in Go?

Thanks

答案1

得分: 19

好的,以下是翻译好的内容:

虽然没有很好的方法来做到这一点,但我认为如果您使用变量而不是函数,代码会更清晰。当您删除else子句时,代码看起来也更整洁。

func paginate(x []int, skip int, size int) []int {
    if skip > len(x) {
        skip = len(x)
    }

    end := skip + size
    if end > len(x) {
        end = len(x)
    }

    return x[skip:end]
}
英文:

Well, while there is no nice way to do it, I think it would be cleaner if you used variables instead of functions. It also looks cleaner when you remove the else clause.

func paginate(x []int, skip int, size int) []int {
	if skip > len(x) {
		skip = len(x)
	}

	end := skip + size
	if end > len(x) {
		end = len(x)
	}

	return x[skip:end]
}

答案2

得分: 11

为什么要限制特定的切片类型?

func 分页(pageNum int, pageSize int, 切片长度 int) (int, int) {
	start := pageNum * pageSize

	if start > 切片长度 {
		start = 切片长度
	}

	end := start + pageSize
	if end > 切片长度 {
		end = 切片长度
	}

	return start, end
}
    start, end := 分页(pageNum, pageSize, len(某个切片))
    分页切片 := 某个切片[start:end]
英文:

Why limit to a certain slice type?

func Paginate(pageNum int, pageSize int, sliceLength int) (int, int) {
	start := pageNum * pageSize

	if start > sliceLength {
		start = sliceLength
	}

	end := start + pageSize
	if end > sliceLength {
		end = sliceLength
	}

	return start, end
}
    start, end := Paginate(pageNum, pageSize, len(someSlice))
    pagedSlice := someSlice[start:end]

答案3

得分: 2

我刚刚发现了go-linq,它受到了Microsoft Linq的启发。

要进行分页,就像我发布的问题中那样,你可以这样写:

From(x).Skip(0).Take(10)

然后你会得到预期的结果。这个库会处理一些问题,比如如果你的Skip和/或Take的值会导致超出范围错误,它会返回一个空的切片。

英文:

I just found go-linq, which is inspired by Microsoft Linq

To paginate, like in the question I posted, you would write:

From(x).Skip(0).Take(10)

And you will get the expected result. This library takes care of returning an empty slice if your Skip and/or Take values would otherwise result in an out of range error.

huangapple
  • 本文由 发表于 2013年12月29日 00:42:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/20816877.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定