You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
︡10985086-f7ce-4dcb-a449-f30f7896001a︡{"html":"<h1>Short variable declarations</h1>\n\n<p>Inside a function, the := short assignment statement can be used in place of a var declaration with implicit type.</p>\n\n<p>Outside a function, every construct begins with a keyword (var, func, and so on) and the := construct is not available.</p>\n"}︡
UnlikeinC, inGoassignmentbetweenitemsofdifferent type requiresanexplicitconversion. Tryremovingthefloat64orintconversionsintheexampleandseewhathappens.
338
338
︡73a905f2-8216-4378-8ba4-4265cc074c19︡{"html":"<h1>Type conversions</h1>\n\n<p>The expression T(v) converts the value v to the type T.</p>\n\n<p>Some numeric conversions:</p>\n\n<pre><code>var i int = 42\nvar f float64 = float64(i)\nvar u uint = uint(f)\n</code></pre>\n\n<p>Or, put more simply:</p>\n\n<pre><code>i := 42\nf := float64(i)\nu := uint(f)\n</code></pre>\n\n<p>Unlike in C, in Go assignment between items of different type requires an explicit conversion. Try removing the float64 or int conversions in the example and see what happens.</p>\n"}︡
︡5785dbe7-55e6-4a53-a87e-c8bcbb26c9c8︡{"html":"<h1>Exercise: Fibonacci closure</h1>\n\n<p>Let’s have some fun with functions.</p>\n\n<p>Implement a fibonacci function that returns a function (a closure) that returns successive fibonacci numbers.</p>\n"}︡
1076
-
︠93dcf2ca-075f-4a91-b180-8bcf1579518b︠
1076
+
︠93dcf2ca-075f-4a91-b180-8bcf1579518bs︠
1077
1077
%go
1078
1078
1079
1079
//fibonacciisafunctionthatreturns
1080
1080
//afunctionthatreturnsanint.
1081
1081
funcfibonacci() func() int {
1082
+
x:=0
1083
+
y:=1
1084
+
returnfunc() int {
1085
+
x,y=y,x+y
1086
+
returnx
1087
+
}
1082
1088
}
1083
1089
1084
1090
funcmain() {
@@ -1087,7 +1093,7 @@ func main() {
1087
1093
fmt.Println(f())
1088
1094
}
1089
1095
}
1090
-
︡f4cae6d7-0264-455f-8cd7-cd13cf6bb229︡{"stderr":"# command-line-arguments\n./ca433c87-2dc0-4f0b-8cfa-994ab0b18de6.go:7: missing return at end of function\n"}︡
Findthecuberootof2, justtomakesurethealgorithmworks. Thereisa [Pow](http://golang.org/pkg/math/cmplx/#Pow) function in the math/cmplx package.
1203
1209
︡4ce5402c-fc34-472a-aebd-ca6198f3ca1f︡{"html":"<h1>Advanced Exercise: Complex cube roots</h1>\n\n<p>Let’s explore Go’s built-in support for complex numbers via the <code>complex64</code> and <code>complex128</code> types. For cube roots, Newton’s method amounts to repeating:\n$$\nz = z- \\frac{z^3-x}{3z^2}\n$$</p>\n\n<p>Find the cube root of 2, just to make sure the algorithm works. There is a <a href=\"http://golang.org/pkg/math/cmplx/#Pow\">Pow</a> function in the math/cmplx package.</p>\n"}︡
1204
-
︠18e1aef0-48bf-4db4-b9c4-ffdc68204bf9︠
1210
+
︠18e1aef0-48bf-4db4-b9c4-ffdc68204bf9s︠
1205
1211
%go
1206
1212
1213
+
import (
1214
+
"fmt"
1215
+
"math/cmplx"
1216
+
)
1217
+
1207
1218
funcCbrt(xcomplex128) complex128 {
1219
+
z:=complex128(2)
1220
+
s:=complex128(0)
1221
+
for {
1222
+
z=z- (cmplx.Pow(z,3) -x)/(3* (z*z))
1223
+
ifcmplx.Abs(s-z) <1e-17 {
1224
+
break
1225
+
}
1226
+
s=z
1227
+
}
1228
+
returnz
1208
1229
}
1209
1230
1210
1231
funcmain() {
1211
1232
fmt.Println(Cbrt(2))
1212
1233
}
1213
-
︡728c04ea-5783-482e-9c11-276683aacc18︡{"stderr":"# command-line-arguments\n./12caf984-f34b-48cb-97be-3c32f3615f38.go:5: missing return at end of function\n"}︡
︡f79d5829-49de-4f62-bd15-447963d64575︡{"html":"<h1>Exercise: Rot13 Reader</h1>\n\n<p>A common pattern is an <code>io.Reader</code> that wraps another <code>io.Reader</code>, modifying the stream in some way.</p>\n\n<p>For example, the <code>gzip.NewReader</code> function takes an <code>io.Reader</code> (a stream of gzipped data) and returns a <code>*gzip.Reader</code> that also implements <code>io.Reader</code> (a stream of the decompressed data).</p>\n\n<p>Implement a <code>rot13Reader</code> that implements <code>io.Reader</code> and reads from an <code>io.Reader</code>, modifying the stream by applying the ROT13 substitution cipher to all alphabetical characters.</p>\n\n<p>The <code>rot13Reader</code> type is provided for you. Make it an <code>io.Reader</code> by implementing its <code>Read</code> method.</p>\n"}︡
︡fc208306-a04b-48a9-be12-462cf4b1d8ce︡{"stderr":"# command-line-arguments\n./cfc58d42-5c15-4fbe-a1fc-cd76012e544f.go:18: cannot use &r (type *rot13Reader) as type io.Reader in function argument:\n\t*rot13Reader does not implement io.Reader (missing Read method)\n"}︡
1687
+
︡34a2c829-73e0-4a6c-a5e1-52348dd91f94︡︡{"stdout":"You cracked the code!","done":false}︡{"done":true}
0 commit comments