forked from mmcgrana/gobyexample
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpointers
200 lines (157 loc) · 9.15 KB
/
pointers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example မြန်မာဘာသာ: Pointers</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'recursion';
}
if (e.key == "ArrowRight") {
window.location.href = 'strings-and-runes';
}
}
</script>
<body>
<div class="example" id="pointers">
<h2><a href="./">Go by Example မြန်မာဘာသာ</a>: Pointers</h2>
<table>
<tr>
<td class="docs">
<p>Go က pointers တွေကို support လုပ်ပါတယ်။
Go မှာ pointer ဆိုတာက variable တစ်ခုရဲ့ memory address ကို သိမ်းထားတဲ့ variable လို့ နားလည်နိုင်ပါတယ်။
Pointer ကိုသုံးပြီးတော့ variable ကို တိုက်ရိုက်တန်ဖိုးမပြင်ဘဲ သွယ်ဝိုက်ပြီး access လုပ်နိုင်ပါတယ်။
Pointer တွေက variable တစ်ခုကို copy မလုပ်ဘဲ reference ကို pass လုပ်ချင်တဲ့အခါမှာ အသုံးဝင်ပါတယ်။</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<a href="https://go.dev/play/p/pBC-DJG5G4A"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<pre class="chroma"><span class="kn">package</span> <span class="nx">main</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kn">import</span> <span class="s">"fmt"</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>pointer နဲ့ value သုံးတာ ဘာကွာခြားလဲဆိုတာကို ပြဖို့အတွက် zeroval ဆိုတဲ့ function နဲ့
zeroptr ဆိုတဲ့ function တွေကို သုံးပြီး ရှင်းပြပါမယ်။ zeroval function က
<code>int</code> parameter ကို လက်ခံပါတယ်။ ဒီမှာ ကျွန်တော်တို့က parameter မှာ value ထည့်ပေးလိုက်ပါတယ်။
zeroval function က ival ရဲ့ copy ကို ရရှိပါတယ်။ ဒီ copy က သူ့ကိုခေါ်တဲ့
function ထဲက ကောင်နဲ့ မတူဘူးဆိုတာကို သတိထားပါ။</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kd">func</span> <span class="nf">zeroval</span><span class="p">(</span><span class="nx">ival</span> <span class="kt">int</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">ival</span> <span class="p">=</span> <span class="mi">0</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>zeroptr function ကတော့ <code>*int</code> parameter ရှိပါတယ်။ ဆိုလိုတာက
သူက <code>int</code> pointer ကို လက်ခံတယ်လို့ ဆိုလိုပါတယ်။ function ထဲမှာရှိတဲ့ <code>*iptr</code> code က
pointer ကို သူ့ရဲ့ memory address ကနေ လက်ရှိတန်ဖိုးဆီကို <em>dereference</em> လုပ်ပါတယ်။
Dereference လုပ်ထားတဲ့ pointer ကို တန်ဖိုးသတ်မှတ်ပေးခြင်းဟာ
ညွှန်းထားတဲ့ address မှာရှိတဲ့ တန်ဖိုးကို ပြောင်းလဲစေပါတယ်။</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kd">func</span> <span class="nf">zeroptr</span><span class="p">(</span><span class="nx">iptr</span> <span class="o">*</span><span class="kt">int</span><span class="p">)</span> <span class="p">{</span>
<span class="o">*</span><span class="nx">iptr</span> <span class="p">=</span> <span class="mi">0</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
<span class="nx">i</span> <span class="o">:=</span> <span class="mi">1</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"initial:"</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"> <span class="nf">zeroval</span><span class="p">(</span><span class="nx">i</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"zeroval:"</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p><code>&i</code> syntax က <code>i</code> ရဲ့ memory address ကိုပေးပါတယ်။
တနည်းအားဖြင့် <code>i</code> ရဲ့ pointer ပါ။</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nf">zeroptr</span><span class="p">(</span><span class="o">&</span><span class="nx">i</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"zeroptr:"</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>Pointer တွေကို Print ထုတ်ကြည့်လို့ရပါတယ်။</p>
</td>
<td class="code">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"pointer:"</span><span class="p">,</span> <span class="o">&</span><span class="nx">i</span><span class="p">)</span>
<span class="p">}</span>
</pre>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
<p><code>zeroval</code> က <code>main</code> ထဲက <code>i</code>
ကို မပြောင်းလဲစေပါဘူး၊ ဒါပေမယ့်
<code>zeroptr</code> ကတော့ ပြောင်းလဲစေပါတယ်။
ဘာကြောင့်လဲဆိုတော့ သူက
အဲဒီ variable ရဲ့ memory address
ကို reference လုပ်ထားလို့ပါ။</p>
</td>
<td class="code">
<pre class="chroma">
<span class="gp">$</span> go run pointers.go
<span class="go">initial: 1
</span><span class="go">zeroval: 1
</span><span class="go">zeroptr: 0
</span><span class="go">pointer: 0x42131100</span></pre>
</td>
</tr>
</table>
<p class="next">
နောက်ဥပမာ: <a href="strings-and-runes">Strings and Runes</a>.
</p>
<p class="footer">
by <a href="https://markmcgranaghan.com">Mark McGranaghan</a> နှင့် <a href="https://eli.thegreenplace.net">Eli Bendersky</a> | <a href="https://github.com/mmcgrana/gobyexample">source</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>
</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func zeroval(ival int) {\u000A ival \u003D 0\u000A}\u000A');codeLines.push('func zeroptr(iptr *int) {\u000A *iptr \u003D 0\u000A}\u000A');codeLines.push('func main() {\u000A i :\u003D 1\u000A fmt.Println(\"initial:\", i)\u000A');codeLines.push(' zeroval(i)\u000A fmt.Println(\"zeroval:\", i)\u000A');codeLines.push(' zeroptr(\u0026i)\u000A fmt.Println(\"zeroptr:\", i)\u000A');codeLines.push(' fmt.Println(\"pointer:\", \u0026i)\u000A}\u000A');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>