Problems with array slicing reliability?

104 views
Skip to first unread message

Mucas Schlack

unread,
Nov 23, 2022, 4:57:03 PM11/23/22
to golang-nuts
Hello Golangers!
Is there anyone who can test the reliability of the following Golang slice feature? I am using an AMG64 computer with Windows 10, and Go version 0.36.0. When I use the following code, to detect and then replace a character at any given length n (9) in a string with an Uppercase Character. Strangely and somehow, the slice pointer will move to another position even though the code is not dynamic and has not changed. Is there anyone who can trouble shoot this code for me?

Please remember that this is "test" code only, so some of it will not make any sense or work. It is only "proof of concept" code.

The code to pay attention to is the following - from lines: 80 - 92!

Many thanks for any help you can give!

Mucas

Example code:

package main

import(
    //"unicode/utf8"
    "math/rand"
    "strconv"
    "strings"
    "fmt"
    "time"
    "os"
)

var alphanumerics = [62] string {
    "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
    "0","1","2","3","4","5","6","7","8","9",
    "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
    }

func main(){

var (
        txtflpth string = "C:\\Workspace\\Transaction Hash.txt"
        flNm string = "Transaction Hash.txt"
        CRLF string = "\n"
        cntstrt string
        AuthKey string
        NwStr string
        cstart int
        cntr int
        flEx *os.FileInfo
        flEx2 *os.File
        trfl *os.File
        err error
    )

    fmt.Print("State where the hash counter should begin:>> ")
    fmt.Scanln(&cntstrt)

    if cntstrt == "" || cntstrt == " " {    
        trfl, err = os.Create(flNm)
       
        if err != nil { panic(err); os.Exit(-1) }
        defer trfl.Close()

    } else {
        var flEx, err = os.Stat(txtflpth)
       
        fmt.Println(flEx.Mode().String)
        //fmt.Println(flEx.IsRegular())
        fmt.Println(flEx.IsDir())
        fmt.Println(flEx.Name())
        fmt.Println(flEx.Size())
        fmt.Println(flEx.Sys())

        if err != nil { if os.IsNotExist(err) { panic(err) } }
       
        cstart, err = strconv.Atoi(cntstrt)
        flEx2, err := os.Open(txtflpth)
        defer flEx2.Close()
    }

    /*defer func(
        if err == trfl.Close() || err == os.IsNotExist(err)!= err { panic(err) }
        if err == flEx.Close() || err == os.IsNotExist(err)!= err { panic(err) }
    )*/

    if cntstrt != "" && cntstrt != " " { cntr = cstart }

    for cntr = cstart; cntr < cstart + 1000; cntr++ {
        NwStr = ""
        AuthKey = NwStr

        for i := 0; i <= 65; i++ {
            time.Sleep(10 * time.Nanosecond)
            rand.Seed(time.Now().UnixNano())
            AuthKey = AuthKey + alphanumerics[rand.Intn(62)] //, [A - Z], [0-9])
        }

        var hashRemap = [...] string {"A","U","G","D","E","X"}
       
        AuthKey = "0x" + AuthKey
        fmt.Println("Transaction Hash: " + strconv.Itoa(cntr) + ") " + AuthKey)
        //AuthKey2 := []rune(AuthKey)

        StrPos := strings.Index(AuthKey, AuthKey[9:10])
        fmt.Printf("The old character %s is at string position: (%d) " + CRLF,AuthKey[9:10], StrPos)
        //os.Exit(1)

        for i := 0; i < 6; i++ {
            switch i {
                case 0: AuthKey = strings.Replace(AuthKey,AuthKey[9:10], hashRemap[0], 1); fmt.Println("Replaced! The New Transaction Hash: " + "(" + strconv.Itoa(i) + ") " + AuthKey); os.Exit(1);
                case 1: AuthKey = strings.Replace(AuthKey,"", hashRemap[1], 1)
                case 2: AuthKey = strings.Replace(AuthKey,"", hashRemap[3], 1)
                case 3: AuthKey = strings.Replace(AuthKey,"", hashRemap[4], 1)
                case 4: AuthKey = strings.Replace(AuthKey,"", hashRemap[5], 1)
                case 5: AuthKey = strings.Replace(AuthKey,"", hashRemap[5], 1)
            }
           
        }

        fmt.Println("Transaction Hash: " + strconv.Itoa(cntr) + ") " + AuthKey)
        if trfl != nil { trfl.WriteString("Transaction Hash: " + strconv.Itoa(cntr) + ") " + AuthKey + CRLF) }
        if flEx != nil { flEx2.WriteString("Transaction Hash: " + strconv.Itoa(cntr) + ") " + AuthKey + CRLF) }

Nikhilesh Susarla

unread,
Nov 23, 2022, 9:12:53 PM11/23/22
to golang-nuts
Before anyone takes a look, can you please place the code in https://go.dev/play/ and then share that link where the code compiles and if it works on the browser and you have specific settings for goarch and goos then gophers can take a look at it. 

Thank you

Kurtis Rader

unread,
Nov 23, 2022, 9:38:30 PM11/23/22
to Mucas Schlack, golang-nuts
You cannot mutate strings. Your code is also missing a final close-brace and possibly has other problems. That your example includes commented out code is also problematic. As Nikhilesh Susarla pointed out you should post a link to the Go playground to avoid any ambiguity.

I added the missing close-brace to your code and observed the following behavior (I entered "1" at the prompt):

> go run x.go                                                              144.6 ms
State where the hash counter should begin:>> 1
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x28 pc=0x102536674]

This tells me there is a fundamental bug in your code having nothing to do with "Problems with array slicing reliability"

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/f27c55aa-836a-453e-a013-a7b752de273bn%40googlegroups.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Amnon

unread,
Nov 24, 2022, 1:35:43 AM11/24/22
to golang-nuts
As others have said, it is worth trying to reduce the program to the minimal version which reproduces your problem
(and then publishing it on https://go.dev/play/ ).

TheDiveO

unread,
Nov 24, 2022, 4:31:24 PM11/24/22
to golang-nuts
On Wednesday, November 23, 2022 at 10:57:03 PM UTC+1 loji...@gmail.com wrote:
[...] I am using an AMG64 computer with Windows 10, and Go version 0.36.0. [emphasis mine]
 
Go version 0.36.0? Really?
Reply all
Reply to author
Forward
0 new messages