Skip to content

Commit a161a24

Browse files
committed
Added
1 parent 62b1680 commit a161a24

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

golang-deferencing.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Title: Golang: The lack of deferencing
2+
Tags: golang,golang-pointers
3+
4+
Whether you use a value or a pointer or reference to value, you do not need to deference, as you do in some languages.
5+
6+
Golang will understand that you want to access the value of the pointer, not the pointer itself.
7+
8+
Say you have this struct.
9+
10+
type NewType struct {
11+
name string
12+
}
13+
14+
Then you make a reference to it with the & operator. You now have a pointer in 'tp'.
15+
16+
var t NewType
17+
tp := &t
18+
19+
You can see below that you can access the attribute of the struct simply with the dot operator - no need to deference.
20+
21+
tp.name = "Hello"
22+
23+
As below, you can still deference if you want. It's just not needed.
24+
25+
fmt.Println(tp.name)
26+
fmt.Println((*tp).name)
27+
28+
The same is true for accessing methods on an interface. 'value.Method()' works the same as 'pointer.Method()'.

0 commit comments

Comments
 (0)