{"id":3548,"date":"2021-07-03T10:45:13","date_gmt":"2021-07-03T02:45:13","guid":{"rendered":"https:\/\/wyxxt.org.cn\/?p=3548"},"modified":"2023-12-04T15:18:51","modified_gmt":"2023-12-04T07:18:51","slug":"go%e8%af%ad%e8%a8%80unsafe","status":"publish","type":"post","link":"https:\/\/wyxxt.org.cn\/?p=3548","title":{"rendered":"go\u8bed\u8a00unsafe"},"content":{"rendered":"<h2>unsafe\u5305<\/h2>\n<p>golang\u662f\u4e00\u79cd\u9759\u6001\u7684\u5f3a\u7c7b\u578b\u7684\u8bed\u8a00\uff0c\u6240\u6709\u7684\u7c7b\u578b\u90fd\u662f\u4e0d\u80fd\u968f\u610f\u8f6c\u6362\u7684\uff0cGo\u8bed\u8a00\u662f\u4e0d\u5141\u8bb8\u4e24\u4e2a\u6307\u9488\u7c7b\u578b\u8fdb\u884c\u8f6c\u6362\u7684\u3002go\u5b98\u65b9\u662f\u4e0d\u63a8\u8350\u4f7f\u7528unsafe\u7684\u64cd\u4f5c\u56e0\u4e3a\u5b83\u662f\u4e0d\u5b89\u5168\u7684\uff0c\u5b83\u7ed5\u8fc7\u4e86golang\u7684\u5185\u5b58\u5b89\u5168\u539f\u5219\uff0c\u5bb9\u6613\u4f7f\u4f60\u7684\u7a0b\u5e8f\u51fa\u73b0\u83ab\u540d\u5176\u5999\u7684\u95ee\u9898\uff0c\u4e0d\u5229\u4e8e\u7a0b\u5e8f\u7684\u6269\u5c55\u4e0e\u7ef4\u62a4\u3002\u4f46\u662f\u5728\u5f88\u591a\u5730\u65b9\u5374\u662f\u5f88\u5b9e\u7528\u3002\u5728\u4e00\u4e9bgo\u5e95\u5c42\u7684\u5305\u4e2dunsafe\u5305\u88ab\u5f88\u9891\u7e41\u7684\u4f7f\u7528\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/oss.wyxxt.org.cn\/images\/2021\/09\/18\/wp_editor_md_6f666d978701af272df641436bded9c4.jpg\" alt=\"\" \/><\/p>\n<h2>unsafe\u5b9a\u4e49<\/h2>\n<pre><code class=\"language-go line-numbers\">package unsafe\n\/\/ArbitraryType\u4ec5\u7528\u4e8e\u6587\u6863\u76ee\u7684\uff0c\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662funsafe\u5305\u7684\u4e00\u90e8\u5206,\u5b83\u8868\u793a\u4efb\u610fGo\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u3002\ntype ArbitraryType int\n\/\/\u4efb\u610f\u7c7b\u578b\u7684\u6307\u9488\uff0c\u7c7b\u4f3c\u4e8eC\u7684*void\ntype Pointer *ArbitraryType\n\/\/\u786e\u5b9a\u7ed3\u6784\u5728\u5185\u5b58\u4e2d\u5360\u7528\u7684\u786e\u5207\u5927\u5c0f\nfunc Sizeof(x ArbitraryType) uintptr\n\/\/\u8fd4\u56de\u7ed3\u6784\u4f53\u4e2d\u67d0\u4e2afield\u7684\u504f\u79fb\u91cf\nfunc Offsetof(x ArbitraryType) uintptr\n\/\/\u8fd4\u56de\u7ed3\u6784\u4f53\u4e2d\u67d0\u4e2afield\u7684\u5bf9\u5176\u503c\uff08\u5b57\u8282\u5bf9\u9f50\u7684\u539f\u56e0\uff09\nfunc Alignof(x ArbitraryType) uintptr\n<\/code><\/pre>\n<h2>\u4f7f\u7528<\/h2>\n<h3>\u7c7b\u578b\u8f6c\u6362<\/h3>\n<pre><code class=\"language-go line-numbers\">func main() {\n    i := 10\n    ip := &amp;i\n    fp := (*float64)(unsafe.Pointer(ip))\n    *fp = *fp * 3\n    fmt.Println(i)\n    fmt.Println(*fp)\n    fmt.Println(reflect.TypeOf(fp))\n}\n<\/code><\/pre>\n<h3>\u6839\u636e\u4f4d\u79fb\u83b7\u53d6\u3001\u4fee\u6539\u5bf9\u8c61\u7684\u5b57\u6bb5<\/h3>\n<pre><code class=\"language-go line-numbers\">type Student struct {\n    Name string\n    Age  int\n}\n\nfunc main() {\n    s := Student{}\n    s.Name = \"Peter\"\n    s.Age = 33\n\n    pStudent := unsafe.Pointer(&amp;s)\n    \/\/ \u6574\u4e2a\u5bf9\u8c61\u8f6c\u6362\u6210\u6307\u9488\uff0c\u9ed8\u8ba4\u662f\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5c5e\u6027\n    name := (*string)(unsafe.Pointer(pStudent))\n    fmt.Println(\"name:\", *name)\n    \/\/ \u5229\u7528Offsetof\u83b7\u53d6age\u5c5e\u6027\u7684\u504f\u79fb\u91cf\u83b7\u53d6\u5c5e\u6027\n    age := (*int)(unsafe.Pointer(uintptr(pStudent) + unsafe.Offsetof(s.Age)))\n    fmt.Println(\"age:\", *age)\n\n    \/\/ \u4fee\u6539\u6307\u9488\u7684\u503c\n    *name = \"Mary\"\n    *age = 20\n    fmt.Println(s)\n}\n<\/code><\/pre>\n<h3>\u83b7\u53d6\u79c1\u6709\u53d8\u91cf<\/h3>\n<pre><code class=\"language-go line-numbers\">type Teacher struct {\n    name string\n    age  int\n}\n\nfunc main() {\n    t := Teacher{\"ttt\", 20}\n\n    pt := unsafe.Pointer(&amp;t)\n    name := (*string)(unsafe.Pointer(pt))\n    fmt.Println(\"name:\", *name)\n}\n<\/code><\/pre>\n<h3>\u6839\u636e sizeof \u51fd\u6570\u83b7\u53d6\u3001\u4fee\u6539<\/h3>\n<pre><code class=\"language-go line-numbers\">func main() {\n    array := []int{0, 1, -2, 3, 4}\n    pointer := &amp;array[0]\n    fmt.Println(*pointer, \" \")\n    memoryAddress := uintptr(unsafe.Pointer(pointer)) + unsafe.Sizeof(array[0])\n    for i := 0; i &lt; len(array)-1; i++ {\n        pointer = (*int)(unsafe.Pointer(memoryAddress))\n        fmt.Print(*pointer, \" \")\n        memoryAddress = uintptr(unsafe.Pointer(pointer)) + unsafe.Sizeof(array[0])\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>unsafe\u5305 golang\u662f\u4e00\u79cd\u9759\u6001\u7684\u5f3a\u7c7b\u578b\u7684\u8bed\u8a00\uff0c\u6240\u6709\u7684\u7c7b\u578b\u90fd\u662f\u4e0d\u80fd\u968f\u610f\u8f6c\u6362\u7684\uff0cGo\u8bed\u8a00\u662f\u4e0d\u5141\u8bb8\u4e24\u4e2a\u6307\u9488\u7c7b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[15],"tags":[398],"class_list":["post-3548","post","type-post","status-publish","format-standard","hentry","category-15","tag-go"],"_links":{"self":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/3548","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3548"}],"version-history":[{"count":2,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/3548\/revisions"}],"predecessor-version":[{"id":3550,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/3548\/revisions\/3550"}],"wp:attachment":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3548"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}