paxPayment, ok = dataObject.(*entities.PassengerPayment)
What are the brackets used for? I'm not sure what is going on in this assignment operation.
Do you need any more details to answer this question?
In order to convert string to integer type in Golang, you can use the following methods. You can use the strconv package's Atoi() function to convert the string into an integer value. Atoi stands for ASCII to integer. The Atoi() function returns two values: the result of the conversion, and the error (if any).
Type assertions in Golang provide access to the exact type of variable of an interface. If already the data type is present in the interface, then it will retrieve the actual data type value held by the interface. A type assertion takes an interface value and extracts from it a value of the specified explicit type.
Type casting is a way of converting data from one data type to another data type. This process of data conversion is also known as type conversion or type coercion. In Java, we can cast both reference and primitive data types. By using casting, data can not be changed but only the data type is changed.
It's a Type assertion. A type assertion can be used to:
Quoting from the spec:
For an expression
x
of interface type and a typeT
, the primary expressionx.(T)
asserts that
x
is notnil
and that the value stored inx
is of typeT
. The notationx.(T)
is called a type assertion.More precisely, if
T
is not an interface type,x.(T)
asserts that the dynamic type ofx
is identical to the typeT
. In this case,T
must implement the (interface) type ofx
; otherwise the type assertion is invalid since it is not possible forx
to store a value of typeT
. IfT
is an interface type,x.(T)
asserts that the dynamic type ofx
implements the interfaceT
.
More specifically your example is a special form of it which also reports whether the type assertion holds. If not, ok
will be false
, and if the assertion holds, ok
will be true
.
This special form never panics unlike the form of:
paxPayment = dataObject.(*entities.PassengerPayment)
Which if dataObject
does not hold a value of type *entities.PassengerPayment
will panic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With