Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slice indices don't work as expected when using int variables #23068

Closed
derFunk opened this issue Dec 9, 2017 · 1 comment
Closed

Slice indices don't work as expected when using int variables #23068

derFunk opened this issue Dec 9, 2017 · 1 comment

Comments

@derFunk
Copy link

derFunk commented Dec 9, 2017

What version of Go are you using (go version)?

go version go1.9.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN="/Users/xxx/Code/bin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/xxx/Code"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.9.2/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.9.2/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/p9/qsjy5d596g7btw_x12l2xn4m0000gn/T/go-build193965940=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

Reading substrings from a fixed width text file into string struct members.
To ease value assignment I'm using a counter variable with with I only define the length of the fixed strings within the text file.
The counter variable is increased by the given length - see inc() function in my example.

Play: https://play.golang.org/p/q3Uqje1JzF

Code:

package main

import (
	"fmt"
)

func main() {
	runes := []rune("abcdefghijk")
	i := 0
		
	fmt.Printf("1.) Expected output: 'ab'. Actual output: '%s'\n", string(runes[0:inc(&i, 2)]))
	fmt.Printf("2.) Expected value of 'i' is '2'. Actual value is %d.\n", i)
	println()
	
	fmt.Printf("3.) Expected output: 'cde'. Actual output: '%s'\n", string(runes[2:inc(&i, 3)]))
	fmt.Printf("4.) Expected value of 'i' is '5'. Actual value is %d.\n", i)
	println()
	
	fmt.Printf("5.) Expected output: 'ef'. Actual output: '%s'\n", string(runes[2:inc(&i, 2)]))
	fmt.Printf("6.) Expected value of 'i' is '7'. Actual value is %d.\n", i)
	println()
	
	fmt.Printf("7.) Expected output: 'ghi'. Actual output: '%s' (<--- Unexpected!)\n", string(runes[i:inc(&i, 3)]))
	fmt.Printf("8.) Expected value of 'i' is '10'. Actual value is %d.\n", i)
	println()
	
}

func inc(counterValue *int, incrementAmount int) int {
	*counterValue = *counterValue + incrementAmount
	return *counterValue
}

Output:

1.) Expected output: 'ab'. Actual output: 'ab'
2.) Expected value of 'i' is '2'. Actual value is 2.

3.) Expected output: 'cde'. Actual output: 'cde'
4.) Expected value of 'i' is '5'. Actual value is 5.

5.) Expected output: 'ef'. Actual output: 'cdefg'
6.) Expected value of 'i' is '7'. Actual value is 7.

7.) Expected output: 'ghi'. Actual output: '' (<--- Unexpected!)
8.) Expected value of 'i' is '10'. Actual value is 10.

As soon as the first slice index value is set to i, an empty slice is returned.

What did you expect to see?

In my example, string(runes[i:inc(&i, 3)]) should have returned ghi.

What did you see instead?

An empty string.

@titanous
Copy link
Member

titanous commented Dec 9, 2017

This is working as intended. The inc on line 23 is called before the slice operation is evaulated, resulting in runes[10:10].

For questions about Go, see https://golang.org/wiki/Questions

@titanous titanous closed this as completed Dec 9, 2017
@golang golang locked and limited conversation to collaborators Dec 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants