Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7499241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:46:00+00:00 2026-05-29T19:46:00+00:00

I’m trying to parse a string from WebSockets connection in Go language. I’m implementing

  • 0

I’m trying to parse a string from WebSockets connection in Go language. I’m implementing both sides of the connection, so the specification of data format is depending only on me.

As this is a simple app (generally for learning purposes), I’ve come up with ActionId Data, where ActionId is a uint8. BackendHandler is a handler for every request in WebSocket Connection.

Platform information

kuba:~$ echo {$GOARCH,$GOOS,`6g -V`}
amd64 linux 6g version release.r60.3 9516

code:

const ( // Specifies ActionId's
  SabPause = iota
)

func BackendHandler(ws *websocket.Conn) {
  buf := make([]byte, 512)
  _, err := ws.Read(buf)
  if err != nil { panic(err.String()) }
  str := string(buf)
  tmp, _ := strconv.Atoi(str[:0])
  data := str[2:]
  fmt.Println(tmp, data)
  switch tmp {
    case SabPause:
      // Here I get `parsing "2": invalid argument`
      // when passing "0 2" to websocket connection
      minutes, ok := strconv.Atoui(data)
      if ok != nil {
        panic(ok.String())
      }
      PauseSab(uint8(minutes))
    default:
      panic("Unmatched input for BackendHandler")
  }
}

All the output: (note the Println that I used for inspecting)

0 2
panic: parsing "2": invalid argument [recovered]
    panic: runtime error: invalid memory address or nil pointer dereference

I couldn’t find the code from which this error is launch, only where the error code is defined (dependent on platform). I’d appreciate general ideas for improving my code, but mainly I just want to solve the conversion problem.

Is this related to my buffer -> string conversion and slice-manipulation(I didn’t want to use SplitAfter methods)?

Edit

This code reproduces the problem:

package main

import (
  "strconv"
  "io/ioutil"
)

func main() {
  buf , _ := ioutil.ReadFile("input")
  str := string(buf)
  _, ok := strconv.Atoui(str[2:])
  if ok != nil {
    panic(ok.String())
  }
}

The file input has to contain 0 2\r\n (depending on the file ending, it may look different on other OSes). This code can be fixed by adding the ending index for reslice, this way:

_, ok := strconv.Atoui(str[2:3])
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:46:01+00:00Added an answer on May 29, 2026 at 7:46 pm

    You didn’t provide a small compilable and runnable program to illustrate your problem. Nor did you provide full and meaningful print diagnostic messages.

    My best guess is that you have a C-style null-terminated string. For example, simplifying your code,

    package main
    
    import (
        "fmt"
        "strconv"
    )
    
    func main() {
        buf := make([]byte, 512)
        buf = []byte("0 2\x00") // test data
        str := string(buf)
        tmp, err := strconv.Atoi(str[:0])
        if err != nil {
            fmt.Println(err)
        }
        data := str[2:]
        fmt.Println("tmp:", tmp)
        fmt.Println("str:", len(str), ";", str, ";", []byte(str))
        fmt.Println("data", len(data), ";", data, ";", []byte(data))
        // Here I get `parsing "2": invalid argument`
        // when passing "0 2" to websocket connection
        minutes, ok := strconv.Atoui(data)
        if ok != nil {
            panic(ok.String())
        }
        _ = minutes
    }
    

    Output:

    parsing "": invalid argument
    tmp: 0
    str: 4 ; 0 2 ; [48 32 50 0]
    data 2 ; 2 ; [50 0]
    panic: parsing "2": invalid argument
    
    runtime.panic+0xac /home/peter/gor/src/pkg/runtime/proc.c:1254
        runtime.panic(0x4492c0, 0xf840002460)
    main.main+0x603 /home/peter/gopath/src/so/temp.go:24
        main.main()
    runtime.mainstart+0xf /home/peter/gor/src/pkg/runtime/amd64/asm.s:78
        runtime.mainstart()
    runtime.goexit /home/peter/gor/src/pkg/runtime/proc.c:246
        runtime.goexit()
    ----- goroutine created by -----
    _rt0_amd64+0xc9 /home/peter/gor/src/pkg/runtime/amd64/asm.s:65
    

    If you add my print diagnostic statements to your code, what do you see?

    Note that your tmp, _ := strconv.Atoi(str[:0]) statement is probably wrong, since str[:0] is equivalent to str[0:0], which is equivalent to the empty string "".

    I suspect that your problem is that you are ignoring the n return value from ws.Read. For example (including diagnostic messages), I would expect,

    buf := make([]byte, 512)
    buf = buf[:cap(buf)]
    n, err := ws.Read(buf)
    if err != nil {
        panic(err.String())
    }
    fmt.Println(len(buf), n)
    buf = buf[:n]
    fmt.Println(len(buf), n)
    

    Also, try using this code to set tmp,

    tmp, err := strconv.Atoi(str[:1])
    if err != nil {
        panic(err.String())
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.