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

Add manipulation function to pointer example #22

Merged
merged 1 commit into from
Oct 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions src/pointer.go
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
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to maintain the issue structure, I would rather deal with this problem in another issue/pull request.

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)
}