Created
October 7, 2020 03:39
-
-
Save ItalyPaleAle/8af067d1bc1eec4fd0f9218acffe0fae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (C) 2020 Alessandro Segala (ItalyPaleAle) | |
// License: MIT | |
// MyGoFunc returns a Go time.Time to JavaScript | |
func MyGoFunc() js.Func { | |
return js.FuncOf(func(this js.Value, args []js.Value) interface{} { | |
// Get the current time as a Go time.Time object | |
now := time.Now() | |
// Get the Date object constructor from JavaScript | |
dateConstructor := js.Global().Get("Date") | |
// Return a new JS "Date" object with the time from the Go "now" variable | |
// We're passing the UNIX timestamp to the "Date" constructor | |
// Because JS uses milliseconds for UNIX timestamp, we need to multiply the timestamp by 1000 | |
return dateConstructor.New(now.Unix() * 1000) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment