golangkorea / devfest2020-golang-community-qna

평소 궁금했던 Go와 관련된 질문들을 커뮤니티 멤버들과 함께 주고 받아요!
https://gdg.community.dev/events/details/google-gdg-golang-korea-presents-devfest-korea-2020-golang-community-qa/
14 stars 0 forks source link

golang 자체에 대한 for 문 질문 드립니다. #12

Closed myungsworld closed 2 years ago

myungsworld commented 2 years ago

이슈는 처음이라 이렇게 쓰는게 맞는지는 모르겠지만 질문드려봅니다..

반복문에서

type Test struct { value bool json:"value" }

func Test() { tests := make([]test , 3 )

// test value 값이 true로 들어감 for i := 0 ; i < len(tests) ; i++ { tests[i].value = true } // test value 값이 true로 들어가지 않음 for _ , test := range tests { test.value = true } }

이 함수에서 값이 for range 구문은 바뀌지 않는데 이유를 알수 있을까요? 인덱스가 없다고해도 값이 바뀌는게 정상적인게 아닌가해서.. 질문드려봅니당..

sikang99 commented 2 years ago

for i , _ := range tests { tests[i].value = true }

로 하면 원하는 값이 들어갈 것입니다. 그냥 값을 받아내면 새로운 test가 생성되어 변경이 되지 않는 것입니다. range의 := 을 이해하시면 될 것 같습니다.

강성일 드림

Sung-IL (Stoney), Kang Email: sikang99@ @.>gmail.com http://gmail.com GTalk: @. Cell: 010-9364-8266 Moto : Challenge and Open the Future!

2021년 9월 24일 (금) 오전 11:52, myungsworld @.***>님이 작성:

이슈는 처음이라 이렇게 쓰는게 맞는지는 모르겠지만 질문드려봅니다..

반복문에서

type Test struct { value bool json:"value" }

func Test() { tests := make([]test , 3 )

// test value 값이 true로 들어감 for i := 0 ; i < len(tests) ; i++ { tests[i].value = true } // test value 값이 true로 들어가지 않음 for _ , test := range tests { test.value = true } }

이 함수에서 값이 for range 구문은 바뀌지 않는데 이유를 알수 있을까요? 인덱스가 없다고해도 값이 바뀌는게 정상적인게 아닌가해서.. 질문드려봅니당..

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/golangkorea/devfest2020-golang-community-qna/issues/12, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACP2OABCFYLX6NK7M764TODUDPRVRANCNFSM5EU7T43Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

myungsworld commented 2 years ago

아하 구조체를 새로 만드는거군요 감사합니다!