Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a nullable field in a struct

Tags:

I'm struggling to do table driven test, and I want do this:

testCases := []struct {     name          string     testUserID    uint     expected      User // <- maybe nil     expectedError error } 

Because the return values of tested function is *User, error.

User is like this, it is defined as DB scheme.

type User struct {   ID uint   CreatedAt time.Time   UpdatedAt time.Time   ... } 

But in this case, I cannot make expected nil.

How can I do this?

Or my approach to do table driven test is wrong?

like image 269
Taichi Avatar asked Aug 24 '18 05:08

Taichi


People also ask

Can struct be nullable?

In C# a struct is a 'value type', which can't be null.

How do you assign a null to a structure in C++?

You cannot ask your struct type to set that pointer to null by itself. You will have to do it explicitly every time you create an object of type struct stack , e.g. struct stack my_stack = { 0 }; Both variants have the same effect - they set to zero all fields of my_stack .

Can I initialize a struct to NULL?

3 answers. It is not possible to set a struct with NULL as it is declared. Fila f = NUll; error: invalid initializer. So cast% from% to NULL , which is not even a type in this code, or assign Fila to a primitive type variable is "wrong".

What are nullable fields?

Nullable types are a feature of some programming languages which allow a value to be set to the special value NULL instead of the usual possible values of the data type.


2 Answers

For empty field you can check for empty values which is zero value which is not nil in case of struct.

When storage is allocated for a variable, either through a declaration or a call of new, or when a new value is created, either through a composite literal or a call of make, and no explicit initialization is provided, the variable or value is given a default value. Each element of such a variable or value is set to the zero value for its type: false for booleans, 0 for numeric types, "" for strings, and nil for pointers, functions, interfaces, slices, channels, and maps.

In your case are using the Struct not a pointer to struct. The value is not nil it is empty though

var user User fmt.Println(user == User{}) // will print true 

But since in your case the returned value is pointer to struct *User, error you can check for nil

var user *User fmt.Println(user == nil) // will print true 

Create struct field that is a pointer.

testCases := []struct {     name          string     testUserID    uint     expected      *User // <- maybe nil     expectedError error } 
like image 56
Himanshu Avatar answered Sep 19 '22 15:09

Himanshu


Go basic types have defined zero values and cannot be nil.

If you want a value to be simply nillable, make it a pointer.

If you do not want a pointer behaviour, you can use null types from third party packages,

e.g. https://github.com/guregu/null

for example int is implemented as:

type Int struct {     Int   int     Valid bool } 

another solution is to write your own struct with nullable value

like image 32
Krzysztof Skolimowski Avatar answered Sep 18 '22 15:09

Krzysztof Skolimowski