How could i execute the .go file using crontab in ubuntu

Hi, I’m new to golang.I just created a programming file using the below code.

package main
import (
	_ "mysql-master"
func main() {
	db, err := sql.Open("mysql", "user:pass@/database?charset=utf8")
	// insert
	stmt, err := db.Prepare("INSERT userinfo SET username=?,departname=?,created=?")
  res, err := stmt.Exec("astaxie", "astaxie", "2012-12-09")
  id, err := res.LastInsertId()

func checkErr(err error) {
    if err != nil {

when I was using the command like this go run insert-data.go, it inserts a new row into the database.

How can I use the same command in ubuntu crontab?. Is it possible?
And i know that there is a proper method for cron, but I want to use it like this.

Help me please

You should build the program into an executable file. This is done by

$ go build insert-data.go

This will produce a file called insert-data that is executable. Put the complete path to this executable into your cron job.

Please also take a look at the basic document How to Write Go Code.


It’s working, Thank you

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.