Skip to main content

All Questions

Tagged with
1 vote
1 answer
63 views

How do you flatten a slice of arrays without extra allocation?

In Go, if I have a slice of arrays of a given type, how can I convert that to just a slice of that same type? Specifically, is it possible to do this without allocating memory for a new backing array? ...
Venning's user avatar
  • 852
-2 votes
1 answer
51 views

How do I append to slice in struct of multidimensional in Go? [duplicate]

New to Go... I have the following: type class struct{ Name string Age int Address string Phone. int } SchoolClassList := [12][4]class{} NewStudent := class{} To start, I initialize ...
Victor's user avatar
  • 1
1 vote
1 answer
48 views

Go ozzo validation. check if a stuct field is in a slice

I have a struct like: type Foo string type Mystruct struct { currentFoo Foo foos []Foo } I want to validate that currentFoo is in foos. I am doing func (s MyStruct) ValidateInput() error { ...
bcsta's user avatar
  • 2,287
1 vote
1 answer
73 views

Go how to only marshal a slice if its not nil. Json showing null for for nil slice

I have a struct in Golang which a slice of my alias type: type Foo struct { NextSet []House `json:"next_set"` } I want to marshal this into a json. When I do not initialise ...
bcsta's user avatar
  • 2,287
-3 votes
1 answer
58 views

does append empty slice to another slice can process allocation in golang?

i have a small question about 'append' function in golang: I have a slice1 and a function which calls in for loop and returns some slice2 which can be empty as well. So the question is how golang will ...
MaksimFeed's user avatar
0 votes
2 answers
59 views

Different Behaviors in Go Race Condition Scenarios

I'm trying to understand why two similar pieces of code behave differently. Both snippets create a large number of goroutines that try to append to the same slice concurrently, which I understand is a ...
Husin Wijaya's user avatar
  • 1,457
2 votes
1 answer
116 views

Why doesn't a byte slice satisfy a generic type constraint of an any slice?

I have the following code with two generic functions. They both do the simple operation of appending a slice to itself and returning the result. However, invoking the simpler and more straightforward ...
Todd's user avatar
  • 259
-4 votes
1 answer
84 views

Taking a slice pointer and returning pointers to that slice's elements

Im currently learning GO and i decided to redo a character ranker i made in python a year ago as an exercise. The program takes user input and makes a slice of structs with them (the struct consists ...
BDealer's user avatar
1 vote
1 answer
55 views

Updating slice after passing to function in Go

I want to use defer in Go while also updating the list I am passing in the function itself. package main import ( "fmt" "time" ) func record(start time.Time, l []int) { ...
lexhuismans's user avatar
1 vote
1 answer
208 views

A compilation error while using builtin func "max" with Go 1.22.1 : "invalid operation: invalid use of ... with built-in max"

I want to use max to get the maximum in slice, and I looked at the function declaration for max: func max[T cmp.Ordered](x T, y ...T) T But even though it was the same as my "myMax" ...
haha wa's user avatar
  • 31
1 vote
2 answers
69 views

how to add all thing in slice to an only one string

hello every body i was coding about create a map and on that map we have 2 thing name of book and number of book and i stuck in one thing if name of book is look like this "Harry Potter" ...
Sepehr's user avatar
  • 69
1 vote
1 answer
119 views

multidimensional slice access performance in golang

I am working on a little and simple game-of-life in Golang and stumbled on a performance problem during slice access. This is the primary data structure for the game: type Neighbor struct { X, Y ...
Tom's user avatar
  • 119
0 votes
1 answer
102 views

GoLang - looping through array of structs - can I map?

I'm new to GoLang, coming from Node. A little late to the (definitely not functional) game and need some help understanding approaches, and perhaps just understanding... I want to omit an item from a ...
Harry Lincoln's user avatar
-1 votes
1 answer
85 views

Dereferencing pointers in a slice does not seem to yield the initial value that the pointer was created from [duplicate]

I need to make a slice of pointers for a Go project I'm working on, that references the items in a slice. The expected behavior would be that I could modify one of the dereferenced items in the slice ...
kanennn's user avatar
  • 44
-3 votes
2 answers
341 views

How to remove duplicate elements from a slice of slices in golang

i have a function which returns a slice of slices. [[-1 -1 2] [-1 0 1] [-1 0 1]] Trying one way of map method wont work because slice cannot be a key. How do i remove the duplicate elements here.
chetan honnavile's user avatar

15 30 50 per page
1
2 3 4 5
91