How to make a nullable field in a struct

c# nullable property
c# nullable struct
c# nullable class
golang nullable string
guregu/null
golang struct required field
nullable definition
golang json null

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?

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
}

Nullable Types in C#, You can declare nullable types using Nullable<t> where T is a type. Nullable<int> i = null; A nullable type can represent the correct range of values for its underlying value type, plus an additional null value. For example, Nullable<int> can be assigned any value from -2147483648 to 2147483647, or a null value. Equals (Object) Indicates whether the current Nullable<T> object is equal to a specified object. Get Hash Code () Retrieves the hash code of the object returned by the Value property. Get Value OrDefault () Retrieves the value of the current Nullable<T> object, or the default value of the underlying type.

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

C#, In C#, the compiler does not allow you to assign a null value to a variable. Nullable<T> struct. this will give compile time error int j = null; // Valid declaration Nullable<int> j = null; // Valid declaration int? j = null; Suppose, in a table a column required null values, then you can use nullable type to enter null values. To add a nullable type by using the Code Editor Add a class to the project. Select the project node in Solution Explorer, and, on the Project menu, click Add Class. In the.cs or.vb file for the new class, add one or more nullable types in the new class to the class declaration.

You can also use special sql type:

type structName struct {
    id sql.NullInt64
    name sql.NullString
    field sql.NullBool
}

Nullable<T> Struct, Represents a value type that can be assigned null. using System; class Sample { // Define the "titleAuthor" table of the Microsoft "pubs" database. public struct� Nullable types can only be used with value types. The Value property will throw an InvalidOperationException if value is null; otherwise it will return the value. The HasValue property returns true if the variable contains a value, or false if it is null. You can only use == and != operators with a nullable type.

Nullable value types, Learn about C# nullable value types and how to use them. For example, a database field may contain true or false , or it may contain m = m2; bool? flag = null; // An array of a nullable value type: int?[] arr = new int?[10]; Use the null- coalescing operator ?? to do that (you can also use the Nullable<T>. StructField ("word", StringType, true) The StructField above sets the name field to "word", the dataType field to StringType, and the nullable field to true. "word" is the name of the column in the DataFrame. StringType means that the column can only take string values like "hello" – it cannot take other values like 34 or false.

Null Values, This means that a null field is indistinguishable from an empty string. variables to Struct members and shows the effect that Null values can have on the Struct. Alter the Column Data Structure. Verify Altered Nullability; Changing the data structure of a column in SQL Server from NULL to NOT NULL, thereby disallowing non-null values in that column, is generally performed using the relatively simple ALTER TABLE syntax to appropriately change the column in question.

Specifying nested and repeated columns | BigQuery, In a STRUCT column, you can also define one or more of the child columns as Repeat these steps to add address ( NULLABLE STRING ), city ( NULLABLE� If you want to use the default value of the underlying value type in place of null, use the Nullable<T>.GetValueOrDefault () method. You can also explicitly cast a nullable value type to a non-nullable type, as the following example shows: C#.

Comments
  • Is SomeType a struct or interface. Please post more code information.
  • Sorry, it is struct.
  • Sorry SomeType is User, and it is struct. I edited my question
  • Then if it's a structure - make it a pointer.
  • @Taichi why do you think you cannot make expected a pointer?