当用户没有输入时返回默认值。

huangapple go评论143阅读模式
英文:

return default value when user not input

问题

当用户没有输入字段时返回默认值
var (
notes ='this will be default'
)

qapi := `
UPDATE custom_param SET param_value = $1, start_date = $2, end_date = $3, status = $4 `enter code here` WHERE param_name = 'default-password'
RETURNING param_value, start_date, end_date, status, notes; 
`
// 插入到 master_param
sqlError := tx.QueryRowContext(ctx, qapi, request.Value, request.StartDate, request.EndDate, request.Status, request.Notes).Scan(
	&value,
	&startDate,
	&endDate,
	&status,
	&notes,
)
queryInsertNotes := `insert `
if request.Notes != "" {
	notes = request.Notes
}

if sqlError != nil {
	log.Println("SQL Error on ReadApplication on UpdateScheduleMaintenance", sqlError)
	tx.Rollback()
	return schedule, sqlError
}

err = tx.Commit()
if err != nil {
	tx.Rollback()
	return schedule, err
}

当用户没有输入任何备注时返回默认备注

英文:

return default value when user not input field
var (
notes ='this will be default'
)

qapi := `
UPDATE custom_param SET param_value = $1, start_date = $2, end_date = $3, status = $4 `enter code here` WHERE param_name = 'default-password'
RETURNING param_value, start_date, end_date, status, notes; 
`
// insrt into master_param
sqlError := tx.QueryRowContext(ctx, qapi, request.Value, request.StartDate, request.EndDate, request.Status, request.Notes).Scan(
	&value,
	&startDate,
	&endDate,
	&status,
	&notes,
)
queryInsertNotes := `insert `
if request.Notes != "" {
	notes = request.Notes
}

if sqlError != nil {
	log.Println("SQL Error on ReadApplication on UpdateScheduleMaintenance", sqlError)
	tx.Rollback()
	return schedule, sqlError
}

err = tx.Commit()
if err != nil {
	tx.Rollback()
	return schedule, err
}

return default notes when user not inputs any notes

答案1

得分: 0

我认为你需要类似这样的代码,但我没有测试过这段代码,不过你可以理解其中的意思。

notes := "this will be default"
if request.Notes != "" {
    notes = request.Notes
}

qapi := `
UPDATE custom_param SET param_value = $1, start_date = $2, end_date = $3, status = $4, notes = $4 WHERE param_name = 'default-password'
RETURNING param_value, start_date, end_date, status, notes; 
`
// 插入到 master_param
sqlError := tx.QueryRowContext(ctx, qapi, request.Value, request.StartDate, request.EndDate, request.Status, notes).Scan(
    &value,
    &startDate,
    &endDate,
    &status,
    &notes,
)


if sqlError != nil {
    log.Println("SQL Error on ReadApplication on UpdateScheduleMaintenance", sqlError)
    tx.Rollback()
    return schedule, sqlError
}

err = tx.Commit()
if err != nil {
    tx.Rollback()
    return schedule, err
}
英文:

I think you need something like this, haven't tested the code but you get the idea.

notes := "this will be default"
if request.Notes != "" {
    notes = request.Notes
}

qapi := `
UPDATE custom_param SET param_value = $1, start_date = $2, end_date = $3, status = $4, notes = $4 WHERE param_name = 'default-password'
RETURNING param_value, start_date, end_date, status, notes; 
`
// insrt into master_param
sqlError := tx.QueryRowContext(ctx, qapi, request.Value, request.StartDate, request.EndDate, request.Status, notes).Scan(
    &value,
    &startDate,
    &endDate,
    &status,
    &notes,
)


if sqlError != nil {
    log.Println("SQL Error on ReadApplication on UpdateScheduleMaintenance", sqlError)
    tx.Rollback()
    return schedule, sqlError
}

err = tx.Commit()
if err != nil {
    tx.Rollback()
    return schedule, err
}

huangapple
  • 本文由 发表于 2021年12月9日 17:39:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/70287611.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定