{"id":237185,"date":"2020-12-31T22:21:49","date_gmt":"2020-12-31T14:21:49","guid":{"rendered":"http:\/\/4563.org\/?p=237185"},"modified":"2020-12-31T22:21:49","modified_gmt":"2020-12-31T14:21:49","slug":"%e4%b8%80%e4%b8%aa%e5%bf%ab%e9%80%9f%e6%8b%b7%e8%b4%9d%e8%a2%ab%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2%e4%b8%ba-interface-%e7%9a%84-struct-%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=237185","title":{"rendered":"\u4e00\u4e2a\u5feb\u901f\u62f7\u8d1d\u88ab\u7c7b\u578b\u8f6c\u6362\u4e3a interface \u7684 struct \u7684\u65b9\u6cd5"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u4e00\u4e2a\u5feb\u901f\u62f7\u8d1d\u88ab\u7c7b\u578b\u8f6c\u6362\u4e3a interface \u7684 struct \u7684\u65b9\u6cd5               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fumeboy <\/span>  <span><i><\/i> 6<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u65b9\u6cd5\u662f \u4ece interface \u83b7\u53d6 struct \u7684\u5730\u5740, \u7136\u540e\u5c06 struct \u8f6c\u6210 []byte\uff0c \u62f7\u8d1d\u540e\u518d\u5c06 []byte \u8f6c\u6210 struct<\/p>\n<pre><code>package main_test  import (  \"fmt\"  \"reflect\"  \"testing\"  \"unsafe\" )  type beCopy struct {  value int }  type emptyInterface struct {  typ  *struct{}  word unsafe.Pointer } type slice struct {  array unsafe.Pointer  len   int  cap   int } func Copy(v interface{}) interface{} {  \/\/ \u4f20\u8fdb\u6765\u7684\u662f\u4e00\u4e2a struct\uff0c \u4f46\u662f\u7c7b\u578b\u8f6c\u6362\u4e3a\u4e86 interface  \/\/ \u76ee\u7684\u662f\u62f7\u8d1d\u8fd9\u4e2a struct  \/\/ \u56e0\u4e3a interface \u672c\u8d28\u662f\u4e00\u4e2a\u643a\u5e26\u4e86\u539f\u6765\u7c7b\u578b\u4fe1\u606f\u7684\u6307\u9488  \/\/ \u6240\u4ee5\u76f4\u63a5 \u503c\u4f20\u9012 \u62f7\u8d1d\u662f\u4e0d\u884c\u7684\uff0c \u503c\u4f20\u9012\u62f7\u8d1d\u53ea\u80fd\u518d\u5f97\u5230\u4e00\u4e2a\u8fd9\u6837\u7684\u6307\u9488  var t = reflect.TypeOf(v)  var length = int(t.Size())   var vslice = []byte{}  \/\/ \u6240\u4ee5\u8fd9\u91cc\u5c06 struct \u7684\u5730\u5740\u66ff\u6362\u5230 slice \u7684\u5730\u5740\u4f4d\uff0c \u4f7f vslice \u6307\u5411\u7684\u4e00\u4e32\u5185\u5b58\u5c31\u662f struct \u7684\u5185\u5b58  (*slice)(unsafe.Pointer(&amp;vslice)).array = (*emptyInterface)(unsafe.Pointer(&amp;v)).word  (*slice)(unsafe.Pointer(&amp;vslice)).len = length  (*slice)(unsafe.Pointer(&amp;vslice)).cap = length   vvslice := make([]byte, length) \/\/ \u518d\u521b\u5efa\u4e00\u4e2a slice  copy(vvslice, vslice) \/\/ \u5c06 struct \u7684\u5185\u5b58\u62f7\u8d1d\u5230\u65b0\u7684 slice   vv := v \/\/ \u62f7\u8d1d\u4e00\u4e2a interface \u6307\u9488  ((*emptyInterface)(unsafe.Pointer(&amp;vv))).word = (*slice)(unsafe.Pointer(&amp;vvslice)).array \/\/ \u5c06 \u65b0 slice \u7684\u5185\u5b58\u5730\u5740 \u66ff\u6362\u4e3a\u7ed3\u6784\u4f53\u6307\u9488\u6307\u5411\u7684\u5730\u5740  return vv \/\/ \u8fd4\u56de\u6df1\u62f7\u8d1d\u540e\u7684 interface \u6307\u9488  \/\/ \u5927\u81f4\u610f\u601d\u5c31\u662f\uff0c\u5c06 struct \u8f6c\u6210 []byte\uff0c \u62f7\u8d1d\u540e\u518d\u5c06 []byte \u8f6c\u6210 struct }  func TestCopy(t *testing.T){  b := beCopy{value: 3}  d := Copy(b)   b.value++  e := Copy(b)  fmt.Println(b,d,e) }  func BenchmarkCopy(b *testing.B) {  c := beCopy{value: 3}  for i := 0; i &lt; b.N; i++ {   Copy(c)  } } <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>5<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4685845\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cholerae <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u4ee3\u7801\u660e\u663e\u6709 bug \u3002\u53ef\u80fd\u8fd0\u6c14\u597d\u6ca1\u6709\u6302\u6389\u3002<br \/>\u4e2d\u95f4 copy \u90a3\u4e00\u6b65\uff0c\u662f\u6ca1\u6709\u5e26 write barrier \u7684\u3002\u8981\u641e\u4e5f\u8981\u7528 typedmemmove \u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4685846\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : fumeboy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @cholerae \u6211\u81ea\u5df1\u4e5f\u611f\u89c9\u4f1a\u6709 GC \u7684\u95ee\u9898 .. typedmemmove \u8fd9\u4e9b\u6211\u53bb\u770b\u4e00\u4e0b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4685847\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wellsc <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u96f6\u62bd\u8c61                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4685848\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : whitehack <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u53ea\u662f\u6d45\u62f7\u8d1d\u5427?\u7ed3\u6784\u5185\u7684\u6307\u9488\u5bf9\u8c61\u8fd8\u662f\u5f15\u7528\u7684\u540c\u4e00\u4e2a?                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4685849\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lance6716 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             ValueOf \u7136\u540e\u518d Copy \uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u4e2a\u5feb\u901f\u62f7\u8d1d\u88ab\u7c7b\u578b\u8f6c\u6362\u4e3a inte&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/237185"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=237185"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/237185\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=237185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=237185"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=237185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}