-
Notifications
You must be signed in to change notification settings - Fork 34
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
Add manipulation function to pointer example #22
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,27 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
b := 255 | ||
var a *int = &b //Saving b's address into variable a | ||
fmt.Printf("Type of a is %T\n", a) | ||
func main() { | ||
b := 255 | ||
var a *int = &b //Saving b's address into variable a | ||
fmt.Printf("Type of a is %T\n", a) | ||
fmt.Println("address of b is", a) | ||
|
||
|
||
// Creating pointers using the new function | ||
// Creating pointers using the new function | ||
|
||
size := new(int) // we can imagine that "new" keyword is a pointer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add correct definition of new as its a built in function that allocates memory and return address of it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As already mentioned, the same applies for this one. I totally agree with you but this pull request/issue simply does not address this topic/problem. In order to maintain the issue structure, I would rather deal with this problem in another issue/pull request. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I really appreciate that! Thank you. Merging this request and will open new issues for improvement of code comments. |
||
fmt.Printf("Size value is %d, type is %T, address is %v\n", *size, size, size) | ||
*size = 85 | ||
fmt.Println("New size value is", *size) | ||
size := new(int) // we can imagine that "new" keyword is a pointer | ||
fmt.Printf("Size value is %d, type is %T, address is %v\n", *size, size, size) | ||
*size = 85 | ||
fmt.Println("New size value is", *size) | ||
// by using pointers when handing over function parameters you can even manipulate parameters and their origin values | ||
manipulatePointerValue(size) | ||
fmt.Println("New size value is", *size) // 170 | ||
} | ||
|
||
|
||
} | ||
// manipulatePointerValue simply just doubles the given input | ||
func manipulatePointerValue(input *int) { | ||
*input = 2 * (*input) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @mmichaelb for the valuable contribution.
I think its better so start with basic things first like how to declare a pointer, what is the significance of '*' and '&' sign. So please add some code comments related to that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I definitely agree with you on this one. However, my pull request/referred issue was really only about the implementation of the pointer function to show another Pointer related feature. Anyway, I am happy to deal with this matter as part of another issue.