I use "unsafe" for performance optimization and with Cgo[1][2][3]. 

I use "reflect" and "unsafe" for doing de-serialization in a serialization 
framework, 
and for cross-language work (Go <-> C, Go <-> scripting languages like 
Python, R, Lua, zygomys)[4][5][6]. 

If you are staying entirely within the Go language, then you aren't likely 
to need "reflect". It lets
you write very dynamic code to handle a Go struct whose layout you don't 
know about at compile
time.  You give up type safety. It is usually alot of painstaking work.

If you aren't profiling your code for performance tuning, you likely won't 
need to mess with "unsafe".
It does let you avoid certain redundant allocations if you can guarantee 
things about your memory's
access patterns.

Since, as the ancient maps used to say, "here be dragons," it is better to 
leave both of them alone until you really, really need them.

examples:

[1] 
https://github.com/glycerine/embedr/blob/96d03afc338c43add0f473ef1ea36ed7257b69f1/r2go.go#L762
[2] 
https://github.com/glycerine/embedr/blob/96d03afc338c43add0f473ef1ea36ed7257b69f1/embedr.go#L255
[3] 
https://github.com/glycerine/greenpack/blob/971e607bf20009e551605ed6a8480335629241f8/msgp/unsafe.go#L34
[4] 
https://github.com/glycerine/greenpack/blob/971e607bf20009e551605ed6a8480335629241f8/msgp/write.go#L775
[5] 
https://github.com/glycerine/zygomys/blob/b4e08088004ee5de19f294d53bb692122463aa10/zygo/callgo.go#L15
[6] https://github.com/glycerine/rmq/blob/master/src/rmq/rmq.go#L466

On Tuesday, May 21, 2024 at 1:44:27 PM UTC+1 Ketan Rathod wrote:

> I have seen many discussions using refflect and unsafe packages, so i am 
> curious to know their usecases and when it is useful to use this packages.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/55e7d611-ca03-436c-a528-d95cb0ab3437n%40googlegroups.com.

Reply via email to