DEV Community

Cover image for Golang perk series : prefix-less functions #2.1 (2023ed.)
Lukas Gaucas
Lukas Gaucas

Posted on • Edited on

Golang perk series : prefix-less functions #2.1 (2023ed.)

"GLOBAL" FUNCTIONS :

Omit the global prefix : window in JS, fmt in Go :

In JavaScript :

function main(){
   /* window. */print()
}
main()
Enter fullscreen mode Exit fullscreen mode

In Golang :

package main 

import (
  . "fmt"
)

func main(){
  /* fmt. */Println("123")
} 
Enter fullscreen mode Exit fullscreen mode

"Struct" is a rough objects in Go :

package main

import (
    . "fmt"
)

type myObject struct{
    firstName string
    lastName string
}

func definePerson(firstName string, lastName string) any {
    // similar to feature in ES6 : if parameters matches object (~struct) fields,.. 
    // ...then we can avoid repeating them, just by defining them as values i.e.:
    return myObject{
        /* firstName:  */firstName,
        /* lastName:  */lastName,
    };
}

func main() {
    res := definePerson("John", "Doe")
    Println(res)
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)