From A Node.js Developer
Soares Chen
@soareschen
3 years experience
http://quiverjs.org
2 months experience
6+ years of experience
Over last 12 years
I am here to learn, just as you are
type Character struct {
Name string
HP, MP, Level int
}
var swordsman *Character
swordsman = &Character{
Name: "dark_knight",
HP: 100,
MP: 30,
Level: 4,
}
swordsman := &Character{
HP: 100,
Level: 4,
}
swordsman.Name // ""
swordsman.MP // 0
type Person struct {
Name string
Age int
Company *Company
}
type Person struct { ... }
func validatePerson(person *Person) error { ... }
func estimateSalary(person *Person) int { ... }
person := &Person{...}
err := validatePerson(person)
salary := estimateSalary(person)
var character, monster *Character
swordsman := createCharacter("dark_knight", ...)
swordsman.receiveExperience(15)
struct character { ... }
interface Character {
GetHP() int
GetMP() int
GetLevel() int
Attack(enemy Character, damage int)
ReceiveExperience(exp int)
}
struct character { ... }
// Note: Not possible
interface Character {
get Name() string
get HP() int
get MP() int
...
}
swordsman = createCharacter("dark_knight", ...)
swordsman.Name // "dark_knight"
struct character { ... }
interface Character {
Name() string
HP() int
MP() int
}
interface Attacker {
Attack(enemy Character, damage int)
}
// Note: Not possible
func (c Character) Attack(enemy Character, damage int) {
...
}
func saveTheWorld(character Character) {
superman, ok := character.(Superman)
if ok {
...
}
}
interface Character { ... }
interface Superhero { ... }
interface Villian { ... }
func giveSuperpower(character Character) Character {
return &characterWithSuperpower{character, ...}
}
func destroyWorld(character Character) {
villian, ok := character.(Villian)
if ok { ... }
}
joker := createVillian(...)
superJoker := giveSuperpower(joker)
destroyWorld(superJoker) // Is superJoker still a Villian?
func createCounter() func() int {
counter := 0
return func() int {
return counter++
}
}
var createCounter = function() {
var counter = 0
return function() {
return counter++
}
}
if(err) return err
func manageUser() error {
db, err := connectDatabase(...)
if err != nil {
return err
}
user, err := getUser(db, ...)
if err != nil {
return err
}
...
}
var manageUser = function(callback) {
connectDatabase(..., function(err, db) {
if(err) return callback(err)
getUser(db, ..., function(err, user) {
if(err) return callback(err)
...
}
}
}
func (ctx *MyHandler) ServeHTTP(
w http.ResponseWriter, req *http.Request, next http.HandlerFunc)
{
...
}
var myHandler = function(request, response, next) {
...
}
import (
user "myapp/user"
post "myapp/post"
}
u := &user.User{...}
p := &post.Post{
User: u,
...
}
var user = require('myapp/user')
var post = require('myapp/post')
var u = new user.User(...)
var p = new post.Post({
user: u,
...
})
import (
userLib "myapp/user"
}
user := &userLib.User{...}
var userLib = require('myapp/user')
var user = new userLib.User(...)
Node
io.js