Create type from slice
I want to create a data type like a stack. I want to add and remove entries "at the top" and be able to print them out. In this example, the xpath type is used to traverse the xml document and keep track of the current path.
So I created an xpath[]string type and wrote the corresponding functions, namely: push() pop() and string().
My problem is that the type loses its state, which confuses me a bit since I thought slices were reference types. Also, if I try to change the function to a pointer receiver, I get several compilation errors. To fix this at this point I just changed the []string to a struct with a single []string field. Although it still bothers me that I can't get it to work using just slice as the base type.
What is the correct approach?
package main import ( "fmt" "strings" ) type xPath []string func (xp xPath) push(entry string) { xp = append(xp, entry) } func (xp xPath) String() string { sb := strings.Builder{} sb.WriteString("/") sb.WriteString(strings.Join(xp, "/")) return sb.String() } func main() { xp := xPath{} xp.push("rss") xp.push("channel") xp.push("items") fmt.Println(xp) // Output: / // Wanted: /rss/channel/items }
Correct answer
Your push function is not doing anything.
Correct push function:
func (xp *xPath) push(entry string) { *xp = append(*xp, entry) }
Slices are reference types in situations where you want to change their value (e.g. using an index).
On the other hand, if you want to reallocate them and replace the entire slice, you should use pointers.
Regarding the stack, there are some better ways: Take a look at this question.
The above is the detailed content of Create type from slice. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The C language function name definition includes: return value type, function name, parameter list and function body. Function names should be clear, concise and unified in style to avoid conflicts with keywords. Function names have scopes and can be used after declaration. Function pointers allow functions to be passed or assigned as arguments. Common errors include naming conflicts, mismatch of parameter types, and undeclared functions. Performance optimization focuses on function design and implementation, while clear and easy-to-read code is crucial.

Yes, H5 page production is an important implementation method for front-end development, involving core technologies such as HTML, CSS and JavaScript. Developers build dynamic and powerful H5 pages by cleverly combining these technologies, such as using the <canvas> tag to draw graphics or using JavaScript to control interaction behavior.

The method of customizing resize symbols in CSS is unified with background colors. In daily development, we often encounter situations where we need to customize user interface details, such as adjusting...

Regarding the reasons and solutions for misaligned display of inline-block elements. When writing web page layout, we often encounter some seemingly strange display problems. Compare...

How to achieve the 45-degree curve effect of segmenter? In the process of implementing the segmenter, how to make the right border turn into a 45-degree curve when clicking the left button, and the point...

How to use JavaScript or CSS to control the top and end of the page in the browser's printing settings. In the browser's printing settings, there is an option to control whether the display is...

Tips for Implementing Segmenter Effects In user interface design, segmenter is a common navigation element, especially in mobile applications and responsive web pages. ...

Dev-C 4.9.9.2 Compilation Errors and Solutions When compiling programs in Windows 11 system using Dev-C 4.9.9.2, the compiler record pane may display the following error message: gcc.exe:internalerror:aborted(programcollect2)pleasesubmitafullbugreport.seeforinstructions. Although the final "compilation is successful", the actual program cannot run and an error message "original code archive cannot be compiled" pops up. This is usually because the linker collects
