Skip to content

Commit 84d2010

Browse files
committed
default values
1 parent 98ab768 commit 84d2010

File tree

6 files changed

+154
-1
lines changed

6 files changed

+154
-1
lines changed

README.md

+104-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ This guide full of examples is intended for people learning Go that are coming f
5353
- [maps](#maps)
5454
- [iteration](#maps)
5555
- [objects](#objects)
56+
- [functions](#functions)
57+
- [default values](#default-values)
5658
- [destructuring](#destructuring)
5759
- [spread operator](#spread-operator)
5860
- [rest operator](#rest-operator)
@@ -103,7 +105,6 @@ This guide full of examples is intended for people learning Go that are coming f
103105
- [message passing](#message-passing)
104106
- [event emitter](#event-emitter)
105107
- [first-class functions](#first-class-functions)
106-
- [default values](#default-values)
107108
-->
108109
- [errors](#errors)
109110
<!--
@@ -1300,6 +1301,108 @@ bar
13001301
bar
13011302
```
13021303

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+
13031406
### destructuring
13041407
---
13051408

examples/default_values.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

examples/default_values.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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)

examples/functions.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
}

examples/functions.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function add(a, b) {
2+
return a + b
3+
}
4+
5+
const x = add(2,3)
6+
console.log(x)

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module github.com/miguelmota/golang-for-nodejs-developers

0 commit comments

Comments
 (0)