Is there a way to have custom typecasting in GoLang?

I know there is typecasting in GoLang, such as:

type long int64;
type ulong uint64;

var l long = 39486;
var i16 = int64(l);
var ui16 = uint64(i16);

fmt.Println( string([]byte {100, 150, 125, 68, 66}) );

But is there a way to add them to inte…


This content originally appeared on DEV Community and was authored by Calin Baenen

I know there is typecasting in GoLang, such as:

type long int64;
type ulong uint64;

var l long = 39486;
var i16 = int64(l);
var ui16 = uint64(i16);

fmt.Println( string([]byte {100, 150, 125, 68, 66}) );

But is there a way to add them to interface/struct types?

type IJType interface {/* ... */}

type JByte struct {/* Implements IJType */}

Is there a way I could make a constructor, like in the following?

var j JByte = JByte(255 /*byte*/); // -> JByte(byte) -> JByte {/* ... */}
byte(j); // 255.


This content originally appeared on DEV Community and was authored by Calin Baenen


Print Share Comment Cite Upload Translate Updates
APA

Calin Baenen | Sciencx (2021-05-21T13:26:48+00:00) Is there a way to have custom typecasting in GoLang?. Retrieved from https://www.scien.cx/2021/05/21/is-there-a-way-to-have-custom-typecasting-in-golang/

MLA
" » Is there a way to have custom typecasting in GoLang?." Calin Baenen | Sciencx - Friday May 21, 2021, https://www.scien.cx/2021/05/21/is-there-a-way-to-have-custom-typecasting-in-golang/
HARVARD
Calin Baenen | Sciencx Friday May 21, 2021 » Is there a way to have custom typecasting in GoLang?., viewed ,<https://www.scien.cx/2021/05/21/is-there-a-way-to-have-custom-typecasting-in-golang/>
VANCOUVER
Calin Baenen | Sciencx - » Is there a way to have custom typecasting in GoLang?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/21/is-there-a-way-to-have-custom-typecasting-in-golang/
CHICAGO
" » Is there a way to have custom typecasting in GoLang?." Calin Baenen | Sciencx - Accessed . https://www.scien.cx/2021/05/21/is-there-a-way-to-have-custom-typecasting-in-golang/
IEEE
" » Is there a way to have custom typecasting in GoLang?." Calin Baenen | Sciencx [Online]. Available: https://www.scien.cx/2021/05/21/is-there-a-way-to-have-custom-typecasting-in-golang/. [Accessed: ]
rf:citation
» Is there a way to have custom typecasting in GoLang? | Calin Baenen | Sciencx | https://www.scien.cx/2021/05/21/is-there-a-way-to-have-custom-typecasting-in-golang/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.