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

x/website: #41941

Closed
sfDoorProBangGit opened this issue Oct 12, 2020 · 1 comment
Closed

x/website: #41941

sfDoorProBangGit opened this issue Oct 12, 2020 · 1 comment
Milestone

Comments

@sfDoorProBangGit
Copy link

@sfDoorProBangGit sfDoorProBangGit commented Oct 12, 2020

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

$ go version

Does this issue reproduce with the latest release? i do not know just check golang with go player

What operating system and processor architecture are you using (go env)? window 10 with Chrom

go env Output
$ go env

What did you do? checking golan slice with for using len() the the condition like "i < len(x) and the result you can see

//golang slice and "for" question
//  when i use len(x) the output is not what i want. 
// because the len(x) change the size auto and the len(x) also change inside for 
// so the problem looks like when "for" condition check repeat not make const when first call for for with repating   
// answer with sfbanghm.yahoo.com

package main


import (
	"fmt"
)

func RmBack (arr []int) ([]int, int) {
	//var arrLen int
	//arrLen = len(arr)
	return arr[:len(arr)-1], arr[len(arr)-1]
	// for front 
	//return arr[1:], arr[0]
}

func main() {

	fmt.Println("Hello, playground")
	
	// slice a[starI: endi]  a(starti- endi] endindex not include
	x := []int {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	px := &x
	xOrg := *px 
	fmt.Printf("xAdd: %p, pxAdd: %p\n", x, *px)
	fmt.Println("xAdd: ",x, "pxAdd: ", *px)
	y := x[4:8] //
	fmt.Println("x: [0 1 2 3 4 5 6 7 8 9 10]")
	fmt.Println("x: ",x, " y: ", y)
	//out=>x: [0 1 2 3 4 5 6 7 8 9 10]
	//out=>x:  [1 2 3 4 5 6 7 8 9 10]  y:  [5 6 7 8]
	fmt.Printf("xadd:%p, yadr:%p, x[4]adr:%p \n",x, y, &x[4])
	//out=>xadd:0xc000012050, yadr:0xc000012070, x[4]adr:0xc000012070 
	fmt.Println(x, len(x)) 
	//if useing len(x) and RmBack some how it dose not work as i except like len(x) 10
	var rmVal int
	
	for i := 0; i < 10 ; i++{
		x, rmVal = RmBack(x)
		fmt.Println("index: ",i,"x: ",x," rmVal: ",rmVal)
	}
	fmt.Println("xAdd: ",x, "pxAdd: ", *px," xorg: ",xOrg)
	
	// using len(u) at condtion of "for" and result is not what I want
	// at the "for" dose not make len(u) as constant at the call of "for" 
	
	u := []int {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	for i := 0; i < len(u) ; i++{
		u, rmVal = RmBack(u)
		fmt.Println("index: ",i,"x: ",u," rmVal: ",rmVal)
	}
	
}

//output

//Hello, playground
//xAdd: 0xc000138000, pxAdd: 0xc000138000
//xAdd:  [1 2 3 4 5 6 7 8 9 10] pxAdd:  [1 2 3 4 5 6 7 8 9 10]
//x: [0 1 2 3 4 5 6 7 8 9 10]
//x:  [1 2 3 4 5 6 7 8 9 10]  y:  [5 6 7 8]
//xadd:0xc000138000, yadr:0xc000138020, x[4]adr:0xc000138020 
//[1 2 3 4 5 6 7 8 9 10] 10
//index:  0 x:  [1 2 3 4 5 6 7 8 9]  rmVal:  10
//index:  1 x:  [1 2 3 4 5 6 7 8]  rmVal:  9
//index:  2 x:  [1 2 3 4 5 6 7]  rmVal:  8
//index:  3 x:  [1 2 3 4 5 6]  rmVal:  7
//index:  4 x:  [1 2 3 4 5]  rmVal:  6
//index:  5 x:  [1 2 3 4]  rmVal:  5
//index:  6 x:  [1 2 3]  rmVal:  4
//index:  7 x:  [1 2]  rmVal:  3
//index:  8 x:  [1]  rmVal:  2
//index:  9 x:  []  rmVal:  1
//xAdd:  [] pxAdd:  []  xorg:  [1 2 3 4 5 6 7 8 9 10]
//index:  0 x:  [1 2 3 4 5 6 7 8 9]  rmVal:  10
//index:  1 x:  [1 2 3 4 5 6 7 8]  rmVal:  9
//index:  2 x:  [1 2 3 4 5 6 7]  rmVal:  8
//index:  3 x:  [1 2 3 4 5 6]  rmVal:  7
//index:  4 x:  [1 2 3 4 5]  rmVal:  6

//Program exited.

What did you expect to see? you can see above two output must to be same

What did you see instead? when slice auto change the len by RmBack function and for condition repeat check len(x) and terminate for loop

@gopherbot gopherbot added this to the Unreleased milestone Oct 12, 2020
@ALTree
Copy link
Member

@ALTree ALTree commented Oct 12, 2020

Hi,

the Go project does not use its bug tracker for general discussion or asking questions about the language. The Github bug tracker is only used for tracking bugs and proposals going through the Proposal Process.

Please see the Questions wiki page; it has a list of good places for asking questions. Thanks!

Closing here, since this is not a bug.

@ALTree ALTree closed this Oct 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.