Runtime error with go after implementing slices

Hey guys,

I am not able to troubleshoot this error. I have created my own slices and I dont seem to find any problems. I do have a runtime error in running it. I have even declared my

var booking = []string

any help would be much appreciated thanks!

The slice is empty, but you acces the first item from it. That’s not possible.

If code was complete and not a screenshot we might help resolving this.

hey man ,

so what should i do now? do i need to screenshot again on the top part?

No, show code, not pics of code.

Use the playground to create a minimal example that recreates the problem, or post the minimal version here, using markdown formatting to mark it as code, or use a pastebin or similar services, but please no pics and not just dump everything…

```go
func main() {}
```

Or

    func main() {}

https://go.dev/play/p/AnbfqC1NZM_B

Hey man, this is the above version of my code.

Sorry for the miscommunication. I was wondering for line 51, there seems to be an error in the syntax.

Would there be any issue right now?

I opened the link and change line 53 from:

var names strings.Fields(booking)

to:

names := strings.Fields(bookings)

And then the program was runnable on the playground:

I get the same error that your reported in your initial post:

panic: runtime error: index out of range [0]

The problem here is that your booking slice is empty (you can tell from the log message in the line above the panic: the whole slice: []) but you try to access the first element on line 40:

fmt.Printf("slice value: %v\n", booking[0])
//                              ^^^^^^^^^^

You can’t access the first element of an empty slice, so the program panics.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.