DEV Community

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

Posted on • Edited on

2 1

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)

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay