slice

How to index a slice element?

与世无争的帅哥 提交于 2020-12-28 04:28:03
问题 I have a slice: Keys []* datastore.Key How could I index one of them in the template file? I guessed {{.Keys[3] }} , but that doesn't work and I searched a lot but with no clue. Any suggestions would be welcome, thanks. 回答1: Use the index command like so: {{index .Keys 3}} 回答2: As stated in the html/template package, the majority of examples are actually located in the text/template pkg docs. See http://golang.org/pkg/text/template/ From the docs index Returns the result of indexing its first

How to convert slice to fixed size array? [duplicate]

二次信任 提交于 2020-12-27 07:52:45
问题 This question already has answers here : How do you convert a slice into an array? (5 answers) Closed 22 days ago . I want to convert a fixed size array from a slice: func gen(bricks []Brick) { if len(bricks) == 16 { if check(Sculpture{bricks}) { var b [16]Brick = bricks[0:16]; } } } But this results in: cannot use bricks[0:16] (type []Brick) as type [16]Brick in assignment How to convert a slice into a fixed size array? 回答1: You need to use copy : slice := []byte("abcdefgh") var arr [4]byte

understanding negative slice step value [duplicate]

喜欢而已 提交于 2020-12-26 03:15:30
问题 This question already has answers here : Understanding slice notation (33 answers) Closed 6 years ago . I am having a problem in understanding what happens when i put negative value to step in case of slicing. I know [::-1] reverses a string. i want to know what value it assign to start and stop to get a reverse. i thought it would be 0 to end to string. and tried f="foobar" f[0:5:-1]---> it gives me no output. why? and i have read start should not pass stop. is that true in case of negative

Return empty list instead of null

邮差的信 提交于 2020-12-25 05:27:32
问题 I want to change my current function to return empty JSON list, currently it returns nil . This is my current code: func (s *Service) projectsGet(c *gin.Context) { var projects []*models.Project user := getUser(c) pag := models.NewPagination(c) ps, err := s.db.ProjectsGet(user.ID, &pag) if err != nil { apiError(c, http.StatusInternalServerError, err) return } projects = ps c.JSON(http.StatusOK, projects) } I want it to return [] , how I can do it? 回答1: A nil slice encodes to a null JSON

Return empty list instead of null

不羁的心 提交于 2020-12-25 05:24:12
问题 I want to change my current function to return empty JSON list, currently it returns nil . This is my current code: func (s *Service) projectsGet(c *gin.Context) { var projects []*models.Project user := getUser(c) pag := models.NewPagination(c) ps, err := s.db.ProjectsGet(user.ID, &pag) if err != nil { apiError(c, http.StatusInternalServerError, err) return } projects = ps c.JSON(http.StatusOK, projects) } I want it to return [] , how I can do it? 回答1: A nil slice encodes to a null JSON

Best method to extract selected columns from 2d array in apps script

落花浮王杯 提交于 2020-12-14 12:39:18
问题 I'm working in google apps script. If I start with a range like range A1:E5, that's a 5x5 array. I want to return range C1:D5, a 5x2 array. Start with a 2d array and return only selected 'columns'. That's basically it. I think it's a fundamental operation, but I'm really struggling. I have my code below, but I'm open to any options that use arrays (not ranges, so as to avoid pinging the server unnecessarily). Note that I do want to be able to pass an array parameter for columns, so [2,3,4] or

Best method to extract selected columns from 2d array in apps script

ぃ、小莉子 提交于 2020-12-14 12:33:25
问题 I'm working in google apps script. If I start with a range like range A1:E5, that's a 5x5 array. I want to return range C1:D5, a 5x2 array. Start with a 2d array and return only selected 'columns'. That's basically it. I think it's a fundamental operation, but I'm really struggling. I have my code below, but I'm open to any options that use arrays (not ranges, so as to avoid pinging the server unnecessarily). Note that I do want to be able to pass an array parameter for columns, so [2,3,4] or

Best method to extract selected columns from 2d array in apps script

早过忘川 提交于 2020-12-14 12:33:05
问题 I'm working in google apps script. If I start with a range like range A1:E5, that's a 5x5 array. I want to return range C1:D5, a 5x2 array. Start with a 2d array and return only selected 'columns'. That's basically it. I think it's a fundamental operation, but I'm really struggling. I have my code below, but I'm open to any options that use arrays (not ranges, so as to avoid pinging the server unnecessarily). Note that I do want to be able to pass an array parameter for columns, so [2,3,4] or

Slice element not updated in go

和自甴很熟 提交于 2020-12-13 20:59:13
问题 I have an account struct as below: type Account struct { Id string Name string Address string City string Email string Phone string Username string Password string IsActive bool } I also have two function: find and update . The find function find certain element from the slice and return the pointer to the element: func find(accounts []Account, username string) *Account { for _, acc := range accounts { if acc.IsActive && acc.Username == username { return &acc } } return nil } The update

Refactor code to use a single channel in an idiomatic way

隐身守侯 提交于 2020-12-13 03:13:10
问题 I have the following code: package main import ( "fmt" "time" ) type Response struct { Data string Status int } func main() { var rc [10]chan Response for i := 0; i < 10; i++ { rc[i] = make(chan Response) } var responses []Response for i := 0; i < 10; i++ { go func(c chan<- Response, n int) { c <- GetData(n) close(c) }(rc[i], i) } for _, resp := range rc { responses = append(responses, <-resp) } for _, item := range responses { fmt.Printf("%+v\n", item) } } func GetData(n int) Response { time