package main import ( "fmt" "github.com/jinzhu/gorm" "log" "time" ) type TaskStatus int type ProcessStatus int type UploadTask struct { ID int `json:"id" gorm:"column:id;primary_key"` SecondID *string `json:"secondId" gorm:"column:second_id;unique_index:second_id"` Ctx string `json:"ctx" gorm:"ctx"` CallID string `json:"callId" gorm:"column:call_id;index:call_id"` ConfID string `json:"confId" gorm:"column:conf_id"` RoomID string `json:"roomId" gorm:"column:room_id"` FromEID string `json:"fromEid" gorm:"column:from_eid"` ServiceID string `json:"serviceId" gorm:"column:service_id"` CreatedAt time.Time `json:"createdAt" gorm:"column:created_at"` UpdatedAt time.Time `json:"updatedAt" gorm:"column:updated_at"` Status TaskStatus `json:"status" gorm:"column:status;index:status"` Processing ProcessStatus `json:"processing" grom:"column:processing;index:processing"` Type string `json:"type" gorm:"column:type;index:type"` AppVersion string `json:"appVersion" gorm:"column:app_version"` InputFiles string `json:"inputFiles" gorm:"column:input_files"` TargetFile string `json:"targetFile" gorm:"column:target_file"` CustomData string `json:"customData" gorm:"column:custom_data"` InternalData string `json:"internalData" gorm:"column:internal_data"` CancelReason string `json:"cancelReason" gorm:"column:cancel_reason"` RetryCount int `json:"retryCount" gorm:"column:retry_count"` InitRetryCount int `json:"initRetryCount" gorm:"column:init_retry_count"` STTRetryCount int `json:"sttRetryCount" gorm:"column:stt_retry_count"` FromVisitorRealm bool `json:"fromVisitorRealm" gorm:"column:from_visitor_realm"` } type Version struct { gorm.Model Version int `gorm:"column:version"` // ; primaryKey Comment string `gorm:"column:comment"` } type VersionTask func(db *gorm.DB) error func insertVersion(db *gorm.DB, version int, comment string) error { tag := "[insertVersion]" // update version var verInfo Version verInfo.Version = 1 verInfo.Comment = comment if err := db.Debug().Create(&verInfo).Error; err != nil { fmt.Printf("%s %v", tag, err) return err } fmt.Printf("%s now version is %d", tag, verInfo.Version) // db.Model(&verInfo).Update(verInfo) return nil } func taskForVersion1(db *gorm.DB) error { fmt.Println("[taskForVersion1] Executing task for version 1") db.Debug().Model(&UploadTask{}).Update("init_retry_count", 15) db.Debug().Model(&UploadTask{}).Update("stt_retry_count", 0) err := insertVersion(db, 1, fmt.Sprintf("updated to version %d", 1)) if err != nil { return err } return nil } func taskForVersion2(db *gorm.DB) error { fmt.Println("[taskForVersion2] Executing task for version 2") return nil } func taskForVersion3(db *gorm.DB) error { fmt.Println("Executing task for version 3") return nil } func main() { latestDBVersion := 1 var versionTasks = []VersionTask{ nil, taskForVersion1, taskForVersion2, taskForVersion3, } // open db db, err := gorm.Open("sqlite3", "cru_test.db") if err != nil { panic("Can't connect") } defer db.Close() // migrate db.Debug().AutoMigrate(&UploadTask{}, &Version{}) // check version var currVerInfo Version //if err := db.Debug().Last(&verInfo).Order("version DESC").Error; err != nil { if err := db.Debug().Raw("SELECT * FROM versions order by version desc LIMIT 1").Scan(&currVerInfo).Error; err != nil { if gorm.IsRecordNotFoundError(err) { fmt.Println("No any row in table 'versions'. Prepare to create.") currVerInfo = Version{Version: 0, Comment: "Initial version"} db.Debug().Create(&currVerInfo) } else { fmt.Println("Query Table[Version] Error.") } } fmt.Println("[Info] Current version:", currVerInfo.Version) for i := currVerInfo.Version + 1; i <= latestDBVersion; i++ { if i < len(versionTasks) { fmt.Printf("[Info] Processing version function %d\n", i) err := versionTasks[i](db) if err != nil { log.Println(err.Error()) } } else { fmt.Printf("No task defined for version %d\n", i) } } }
Write, Run & Share Go code online using OneCompiler's Go online compiler for free. It's one of the robust, feature-rich online compilers for Go language, running on the latest version 1.10.2. Getting started with the OneCompiler's Go compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as GO
and start coding.
OneCompiler's Go online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Go program which takes name as input and prints hello message with your name.
package main
import "fmt"
func main() {
var name string
fmt.Scanf("%s", &name)
fmt.Printf("Hello %s", name)
}
Go language is an open-source, statically typed programming language by Google. Go is highly recommended for creation of highly scalable and available web applications.
Some of the products developed using Go are Kubernetes, Docker, Dropbox, Infoblox etc.
Data type | Description | Size | Range |
---|---|---|---|
uint8 | 8-bit unsigned integer | 1 byte | 0 to 255 |
int8 | 8-bit signed integer | 1 byte | -128 to 127 |
int16 | 16-bit signed integer | 2 bytes | -32768 to 32767 |
unit16 | 16-bit unsigned integer | 2 bytes | 0 to 65,535 |
int32 | 32-bit signed integer | 4 bytes | -2,147,483,648 to 2,147,483,647 |
uint32 | 32-bit unsigned integer | 4 bytes | 0 to 4,294,967,295 |
int64 | 64-bit signed integer | 8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
uint64 | 64-bit unsigned integer | 8 bytes | 0 to 18,446,744,073,709,551,615 |
float32 | 32-bit signed floating point number | 4 bytes | ±1.5e-45 to ±3.4e38 |
float | 64-bit signed floating point number | 8 bytes | ±5.0e-324 to ±1.7e308 |
string | sequence of immutable text | ||
bool | Stores either true or false | 1 byte | True or false |
Variable is a name given to the storage area in order to manipulate them in our programs.
var varible-names datatype;
When ever you want to perform a set of operations based on a condition or set of conditions then If or IF-ELSE or Nested If-Elif-Else are used.
if(conditional-expression) {
// code
}
if(conditional-expression) {
// code
} else {
// code
}
if(conditional-expression) {
// code
} else if(conditional-expression) {
// code
} else {
// code
}
For loop is used to iterate a set of statements based on a condition.
for Initialization; Condition; Increment/decrement {
// code
}
Switch is an alternative to If-Else-If ladder.
switch conditional-expression {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
// code to be executed when all the above cases are not matched;
}
Go doesn't have while or do-while loops like in C.
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
func functionname(parameter-name type) returntype {
//code
}