File tree 6 files changed +154
-1
lines changed
6 files changed +154
-1
lines changed Original file line number Diff line number Diff line change @@ -53,6 +53,8 @@ This guide full of examples is intended for people learning Go that are coming f
53
53
- [ maps] ( #maps )
54
54
- [ iteration] ( #maps )
55
55
- [ objects] ( #objects )
56
+ - [ functions] ( #functions )
57
+ - [ default values] ( #default-values )
56
58
- [ destructuring] ( #destructuring )
57
59
- [ spread operator] ( #spread-operator )
58
60
- [ rest operator] ( #rest-operator )
@@ -103,7 +105,6 @@ This guide full of examples is intended for people learning Go that are coming f
103
105
- [message passing](#message-passing)
104
106
- [event emitter](#event-emitter)
105
107
- [first-class functions](#first-class-functions)
106
- - [default values](#default-values)
107
108
-->
108
109
- [ errors] ( #errors )
109
110
<!--
@@ -1300,6 +1301,108 @@ bar
1300
1301
bar
1301
1302
```
1302
1303
1304
+ ### functions
1305
+ ---
1306
+
1307
+ #### Node.js
1308
+
1309
+ ``` node
1310
+ function add (a , b ) {
1311
+ return a + b
1312
+ }
1313
+
1314
+ const x = add (2 ,3 )
1315
+ console .log (x)
1316
+ ```
1317
+
1318
+ Output
1319
+
1320
+ ``` bash
1321
+ 5
1322
+ ```
1323
+
1324
+ #### Go
1325
+
1326
+ ``` go
1327
+ package main
1328
+
1329
+ import " fmt"
1330
+
1331
+ func add (a int , b int ) int {
1332
+ return a + b
1333
+ }
1334
+
1335
+ func main () {
1336
+ x := add (2 , 3 )
1337
+ fmt.Println (x)
1338
+ }
1339
+ ```
1340
+
1341
+ Output
1342
+
1343
+ ``` bash
1344
+ 5
1345
+ ```
1346
+
1347
+ ### default values
1348
+ ---
1349
+
1350
+ #### Node.js
1351
+
1352
+ ``` node
1353
+ function greet (name = ' stranger' ) {
1354
+ return ` hello ${ name} `
1355
+ }
1356
+
1357
+ let message = greet ()
1358
+ console .log (message)
1359
+
1360
+ message = greet (' bob' )
1361
+ console .log (message)
1362
+ ```
1363
+
1364
+ Output
1365
+
1366
+ ``` bash
1367
+ hello stranger
1368
+ hello bob
1369
+ ```
1370
+
1371
+ #### Go
1372
+
1373
+ use pointers and check for nil to know if explicitly left blank
1374
+
1375
+ ``` go
1376
+ package main
1377
+
1378
+ import " fmt"
1379
+
1380
+ func greet (name *string ) string {
1381
+ n := " stranger"
1382
+ if name != nil {
1383
+ n = *name
1384
+ }
1385
+
1386
+ return fmt.Sprintf (" hello %s " , n)
1387
+ }
1388
+
1389
+ func main () {
1390
+ message := greet (nil )
1391
+ fmt.Println (message)
1392
+
1393
+ name := " bob"
1394
+ message = greet (&name)
1395
+ fmt.Println (message)
1396
+ }
1397
+ ```
1398
+
1399
+ Output
1400
+
1401
+ ``` bash
1402
+ hello stranger
1403
+ hello bob
1404
+ ```
1405
+
1303
1406
### destructuring
1304
1407
---
1305
1408
Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import "fmt"
4
+
5
+ func greet (name * string ) string {
6
+ n := "stranger"
7
+ // use pointers and check for nil to know if explicitly left blank
8
+ if name != nil {
9
+ n = * name
10
+ }
11
+
12
+ return fmt .Sprintf ("hello %s" , n )
13
+ }
14
+
15
+ func main () {
16
+ message := greet (nil )
17
+ fmt .Println (message )
18
+
19
+ name := "bob"
20
+ message = greet (& name )
21
+ fmt .Println (message )
22
+ }
Original file line number Diff line number Diff line change
1
+ function greet ( name = 'stranger' ) {
2
+ return `hello ${ name } `
3
+ }
4
+
5
+ let message = greet ( )
6
+ console . log ( message )
7
+
8
+ message = greet ( 'bob' )
9
+ console . log ( message )
Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import "fmt"
4
+
5
+ func add (a int , b int ) int {
6
+ return a + b
7
+ }
8
+
9
+ func main () {
10
+ x := add (2 , 3 )
11
+ fmt .Println (x )
12
+ }
Original file line number Diff line number Diff line change
1
+ function add ( a , b ) {
2
+ return a + b
3
+ }
4
+
5
+ const x = add ( 2 , 3 )
6
+ console . log ( x )
Original file line number Diff line number Diff line change
1
+ module github.com/miguelmota/golang-for-nodejs-developers
You can’t perform that action at this time.
0 commit comments