1

I have such struct in Golang application:

type Employee struct {
    ID int `json:"employee_id"`
    Email *string `json:"employee_email"`
    LastName *string `json:"employee_last_name"`
    FirstName *string `json:"employee_first_name"`
    Sex *string `json:"employee_sex"`
}

Some string fields of this struct can be empty. If I use *string application return me "". If use sql.NullString it return me for example such result:

"employee_last_name": {
    String: "",
    Valid: true
}

I want to show null is string field is empty.

In documentation I found such code:

type NullString struct {
    String string
    Valid  bool
}

func (ns *NullString) Scan(value interface{}) error {
    if value == nil {
        ns.String, ns.Valid = "", false
        return nil
    }
    ns.Valid = true
    return convertAssign(&ns.String, value)
}

func (ns NullString) Value() (driver.Value, error) {
    if !ns.Valid {
        return nil, nil
    }
    return ns.String, nil
}

As I understand this code could help me to solve my problem, right?! What I need to import in my application to use convertAssign function?

12
  • I assume you are getting data in from a SQL source? And the table data in question has a null value, and you ware asking why the null, does not appear as a go nil value? Is that a correct assessment of you question? Commented Mar 13, 2019 at 19:54
  • and if it's an SQL - please post the DB driver. MySQL, SQLite etc? There may be driver options that need to be enabled to support NULL values during a RowScan. Commented Mar 13, 2019 at 19:57
  • @colminator hello! Yes, you are right. For example I notice that *int return me null. Why *string return me just "" instead of null. In fact in database colomn has null value. Can I change this default behavior? Commented Mar 13, 2019 at 19:58
  • 2
    @colminator note that this has nothing to do with goracle, as you've already pointed out in your answer by quoting the documentation, this is an issue with oracle proper, not the driver. If oracle internally changes an empty string to NULL there's little the authors of the driver can do. Also stackoverflow.com/a/13278879/965900 Commented Mar 13, 2019 at 20:37
  • 1
    @colminator nobody said anything about oracle db not supporting NULLs. Commented Mar 13, 2019 at 20:49

2 Answers 2

2

In the Go language specification nil is not a valid value for type string. the default, zero value is "".

The top answer for this question goes into more detail.

Sign up to request clarification or add additional context in comments.

1 Comment

Hello! Thank you for information. For example I notice that *int return me null. In the same time *string return me just "" instead of null. In fact in database colomn has null value. Is it possible to change this default behavior?
1

Edit:

What you want to do is not possible according to the oracle go driver docs:

sql.NullString

sql.NullString is not supported: Oracle DB does not differentiate between an empty string ("") and a NULL

Original Answer:

Need more details on the SQL database type you are connecting to. I know the go SQLite3 pkg - github.com/mattn/go-sqlite3 - supports setting nil for values of type *string.

Check the details of the driver you are using to connect to the database. For instance with MySQL, getting SQL date fields into go's native time.Time struct type is not set by default - it must be turned on at the driver level.

1 Comment

Hello! Thank you for your answer. In my case I use Oracle database. I use goracle package to work with database. Do you have any other ideas now?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.