0

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?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Nurzhan Nogerbek
  • 4,806
  • 16
  • 87
  • 193
  • 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? – colm.anseo Mar 13 '19 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. – colm.anseo Mar 13 '19 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? – Nurzhan Nogerbek Mar 13 '19 at 19:58
  • @colminator I use `Oracle` database. I use [goracle](https://github.com/go-goracle/goracle) package to work with database. – Nurzhan Nogerbek Mar 13 '19 at 20:09
  • it does not appear that is is possible then - see my updated answer. – colm.anseo Mar 13 '19 at 20:17
  • Thank you for the information. That's sad. Is it really impossible somehow intercept the value and check it at the `Scan` function level? – Nurzhan Nogerbek Mar 13 '19 at 20:22
  • The driver could do it - but looks like the `goracle` authors deemed it too confusing. Sorry! – colm.anseo Mar 13 '19 at 20:26
  • Well, thank you for your help! Can I ask you one more question?! What you mean by `... driver could do it ...`? I am little bit confused. – Nurzhan Nogerbek Mar 13 '19 at 20:35
  • 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 https://stackoverflow.com/a/13278879/965900 – mkopriva Mar 13 '19 at 20:37
  • goracle is an open source driver. It can be changed. This feature could be added. You'd just have to convince the authors - or fork and do it yourself! – colm.anseo Mar 13 '19 at 20:37
  • I have no experience with Oracle DB's - but I have to assume very much *CAN* support NULLs in their tables. Up to drivers/clients if they choose to support these. – colm.anseo Mar 13 '19 at 20:41
  • 1
    @colminator nobody said anything about oracle db not supporting NULLs. – mkopriva Mar 13 '19 at 20:49

2 Answers2

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.

Yu-s-uF
  • 85
  • 1
  • 8
  • 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? – Nurzhan Nogerbek Mar 13 '19 at 20:02
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.

Community
  • 1
  • 1
colm.anseo
  • 19,337
  • 4
  • 43
  • 52
  • Hello! Thank you for your answer. In my case I use `Oracle` database. I use [goracle](https://github.com/go-goracle/goracle) package to work with database. Do you have any other ideas now? – Nurzhan Nogerbek Mar 13 '19 at 20:11