💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# 下划线 ### 下划线的特殊性 1. 在import中使用 2. ``` <pre class="calibre15">``` 在代码中使用 ``` ``` 3. ``` <pre class="calibre13">``` <span class="token2">package</span> main <span class="token2">import</span> <span class="token1">(</span> <span class="token4">"os"</span> <span class="token1">)</span> func <span class="token3">main</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> buf <span class="token1">:</span><span class="token">=</span> <span class="token3">make</span><span class="token1">(</span><span class="token1">[</span><span class="token1">]</span>byte<span class="token1">,</span> <span class="token5">1024</span><span class="token1">)</span> f<span class="token1">,</span> _ <span class="token1">:</span><span class="token">=</span> os<span class="token1">.</span><span class="token3">Open</span><span class="token1">(</span><span class="token4">"/Users/***/Desktop/text.txt"</span><span class="token1">)</span> defer f<span class="token1">.</span><span class="token3">Close</span><span class="token1">(</span><span class="token1">)</span> <span class="token2">for</span> <span class="token1">{</span> n<span class="token1">,</span> _ <span class="token1">:</span><span class="token">=</span> f<span class="token1">.</span><span class="token3">Read</span><span class="token1">(</span>buf<span class="token1">)</span> <span class="token2">if</span> n <span class="token">==</span> <span class="token5">0</span> <span class="token1">{</span> <span class="token2">break</span> <span class="token1">}</span> os<span class="token1">.</span>Stdout<span class="token1">.</span><span class="token3">Write</span><span class="token1">(</span>buf<span class="token1">[</span><span class="token1">:</span>n<span class="token1">]</span><span class="token1">)</span> <span class="token1">}</span> <span class="token1">}</span> ``` ``` ``` <pre class="calibre13">``` 下划线意思是忽略这个变量<span class="token1">.</span> 比如os<span class="token1">.</span>Open,返回值为<span class="token">*</span>os<span class="token1">.</span>File,error 普通写法是f<span class="token1">,</span>err <span class="token1">:</span><span class="token">=</span> os<span class="token1">.</span><span class="token3">Open</span><span class="token1">(</span><span class="token4">"xxxxxxx"</span><span class="token1">)</span> 如果此时不需要知道返回的错误值 就可以用f<span class="token1">,</span> _ <span class="token1">:</span><span class="token">=</span> os<span class="token1">.</span><span class="token3">Open</span><span class="token1">(</span><span class="token4">"xxxxxx"</span><span class="token1">)</span> 如此则忽略了error变量 ``` ``` ``` <pre class="calibre13">``` 占位符,意思是那个位置本应赋给某个值,但是咱们不需要这个值。 所以就把该值赋给下划线,意思是丢掉不要。 这样编译器可以更好的优化,任何类型的单个值都可以丢给下划线。 这种情况是占位用的,方法返回两个结果,而你只想要一个结果。 那另一个就用 <span class="token4">"_"</span> 占位,而如果用变量的话,不使用,编译器是会报错的。 ``` ``` ``` <pre class="calibre13">``` <span class="token2">import</span> <span class="token4">"database/sql"</span> <span class="token2">import</span> _ <span class="token4">"github.com/go-sql-driver/mysql"</span> ``` ``` 第二个import就是不直接使用mysql包,只是执行一下这个包的init函数,把mysql的驱动注册到sql包里,然后程序里就可以使用sql包来访问mysql数据库了。