You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
The text was updated successfully, but these errors were encountered:
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
)?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:
Output:
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 returnedghi
.What did you see instead?
An empty string.
The text was updated successfully, but these errors were encountered: