default values
Node.js
function greet(name = 'stranger') {
return `hello ${name}`
}
let message = greet()
console.log(message)
message = greet('bob')
console.log(message)
Output
hello stranger
hello bob
Go
use pointers and check for nil to know if explicitly left blank
package main
import "fmt"
func greet(name *string) string {
n := "stranger"
if name != nil {
n = *name
}
return fmt.Sprintf("hello %s", n)
}
func main() {
message := greet(nil)
fmt.Println(message)
name := "bob"
message = greet(&name)
fmt.Println(message)
}
Output
hello stranger
hello bob
当前内容版权归 miguelmota 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 miguelmota .