mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2026-01-16 04:07:13 +08:00
rebuild
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
<title>顯示一個類型的方法集 | Go语言圣经</title>
|
||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
||||
<meta name="description" content="">
|
||||
<meta name="generator" content="GitBook 2.5.2">
|
||||
<meta name="generator" content="GitBook 2.6.6">
|
||||
|
||||
|
||||
<meta name="HandheldFriendly" content="true"/>
|
||||
@@ -48,7 +48,13 @@
|
||||
<body>
|
||||
|
||||
|
||||
<div class="book" data-level="12.8" data-chapter-title="顯示一個類型的方法集" data-filepath="ch12/ch12-08.md" data-basepath=".." data-revision="Thu Dec 31 2015 16:18:40 GMT+0800 (中国标准时间)">
|
||||
<div class="book"
|
||||
data-level="12.8"
|
||||
data-chapter-title="顯示一個類型的方法集"
|
||||
data-filepath="ch12/ch12-08.md"
|
||||
data-basepath=".."
|
||||
data-revision="Sat Jan 02 2016 16:00:23 GMT+0800 (中国标准时间)"
|
||||
data-innerlanguage="">
|
||||
|
||||
|
||||
<div class="book-summary">
|
||||
@@ -2024,7 +2030,40 @@
|
||||
<section class="normal" id="section-">
|
||||
|
||||
<h2 id="128-顯示一個類型的方法集">12.8. 顯示一個類型的方法集</h2>
|
||||
<p>TODO</p>
|
||||
<p>我們的最後一個例子是使用reflect.Type來打印任意值的類型和枚舉它的方法:</p>
|
||||
<pre><code class="lang-Go">gopl.io/ch12/methods
|
||||
|
||||
<span class="hljs-comment">// Print prints the method set of the value x.</span>
|
||||
<span class="hljs-keyword">func</span> Print(x <span class="hljs-keyword">interface</span>{}) {
|
||||
v := reflect.ValueOf(x)
|
||||
t := v.Type()
|
||||
fmt.Printf(<span class="hljs-string">"type %s\n"</span>, t)
|
||||
|
||||
<span class="hljs-keyword">for</span> i := <span class="hljs-number">0</span>; i < v.NumMethod(); i++ {
|
||||
methType := v.Method(i).Type()
|
||||
fmt.Printf(<span class="hljs-string">"func (%s) %s%s\n"</span>, t, t.Method(i).Name,
|
||||
strings.TrimPrefix(methType.String(), <span class="hljs-string">"func"</span>))
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
<p>reflect.Type和reflect.Value都提供了一個Method方法。每次t.Method(i)調用將一個reflect.Method的實例,對應一個用於描述一個方法的名稱和類型的結構體。每次v.Method(i)方法調用都返迴一個reflect.Value以表示對應的值(§6.4),也就是一個方法是幫到它的接收者的。使用reflect.Value.Call方法(我們之類沒有演示),將可以調用一個Func類型的Value,但是這個例子中隻用到了它的類型。</p>
|
||||
<p>這是屬於time.Duration和<code>*strings.Replacer</code>兩個類型的方法:</p>
|
||||
<pre><code class="lang-Go">methods.Print(time.Hour)
|
||||
<span class="hljs-comment">// Output:</span>
|
||||
<span class="hljs-comment">// type time.Duration</span>
|
||||
<span class="hljs-comment">// func (time.Duration) Hours() float64</span>
|
||||
<span class="hljs-comment">// func (time.Duration) Minutes() float64</span>
|
||||
<span class="hljs-comment">// func (time.Duration) Nanoseconds() int64</span>
|
||||
<span class="hljs-comment">// func (time.Duration) Seconds() float64</span>
|
||||
<span class="hljs-comment">// func (time.Duration) String() string</span>
|
||||
|
||||
methods.Print(<span class="hljs-built_in">new</span>(strings.Replacer))
|
||||
<span class="hljs-comment">// Output:</span>
|
||||
<span class="hljs-comment">// type *strings.Replacer</span>
|
||||
<span class="hljs-comment">// func (*strings.Replacer) Replace(string) string</span>
|
||||
<span class="hljs-comment">// func (*strings.Replacer) WriteString(io.Writer, string) (int, error)</span>
|
||||
<span class="hljs-string">`
|
||||
</span></code></pre>
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
Reference in New Issue
Block a user