fmt and dependencies

agolangf · · 460 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Ok, so I am new to programming in general with a little experience in HTML/CSS, Javascript and Python so go easy on me. I ordered An Introduction to Programming in Go, but it hasn&#39;t arrived yet, so I just went to a tour of Go to start practicing and I&#39;ve been looking up some pretty advanced things to me that don&#39;t really make a lot of sense, but is probably helpful. Anyways, I have looked up the fmt thing, but I don&#39;t really understand what they are talking about. When I put import (&#34;fmt&#34;) or some other thing like import (&#34;math&#34;) what is happening exactly and why am I using fmt.Println(&#34;Hello, World&#34;) instead of println? Am I uploading a library of something? Could someone explain it in layman&#39;s terms as well as provide a simple example? Also, what are dependencies? Thanks!</p> <hr/>**评论:**<br/><br/>justinisrael: <pre><p>If you know a little bit of Python, you can start by thinking about modules. In Python, you use the import keyword to bring in code from the file system, and make its functionality available to your current file. When this code is loaded into your file, it ends up in a namespace to avoid clashing with things in your file that may potentially be named the same. Because python is a dynamic interpreted language, those imports are being performed at runtime when you execute a python program. </p> <p>Now let&#39;s talk about Go, which also has the import keyword. In Go, you are importing external code organized as packages instead of modules. This is just the Go vs Python terminology. But a big difference is that Go is a compiled language. In this case, the code is only looked up from the filesystem once, when you compile your binary. After that, it is part of the binary already when you run it. But similar to python, when you import code, you get access to it through a namespace to avoid clashes. </p> <p>The &#34;fmt&#34; package happens to be some code that is part of the Go standard library. It organizes logic related to formatting and printing strings. If you need that functionality, then you import it and opt to make it included in your compiled binary. It is possible that you may not need to print formatted output so you could choose not to import things you don&#39;t need. </p> <p><em>Edit</em></p> <p>And if it wasn&#39;t clear from the above explanation, a dependency is code that is external to your package, that you want to import and make use of. You now are dependant on this external code to continue existing, and to not change in ways that will break the way you are using it. These are things that can prevent your code from compiling in the future. So you establish trust with the source of your dependencies. </p></pre>liahkim3: <pre><p>Ok... it&#39;s starting to make sense. With: &#34;Package fmt implements formatted I/O with functions analogous to C&#39;s printf and scanf. The format &#39;verbs&#39; are derived from C&#39;s but are simpler.&#34; does that just mean that by using the fmt package, you are using a more complex Println function without having to write out extra long code? Are the verbs just what can be printed? Sorry, I am incredibly new and trying to wrap my head around it. I am trying to figure out what is happening when I attach fmt with Println to fmt.Println. Am I just adding additional functionality to Println? Thank you so much for your previous response. </p></pre>justinisrael: <pre><p>The &#34;verbs&#34; are just symbols that you can use as placeholders in the format string, so that your additional arguments can be &#34;formatted&#34; into their place. Again if you are familiar enough with Python, there is a similar thing with being able to do this in Python: </p> <p>&#34;Hello %s&#34; % &#34;World!&#34; </p> <p>or </p> <p>&#34;Hello {0}&#34;.format(&#34;World!&#34;)</p> <p>But in Go they are telling you that their approach to the formatting verbs is similar to C. And yes, those verbs are a convenience for doing different types of formatting to the objects you provide.</p></pre>liahkim3: <pre><p>I got it. Println is held within fmt.</p></pre>nathj07: <pre><p>Following on from justinisrael and the point &#34;It is possible that you may not need to print formatted output so you could choose not to import things you don&#39;t need.&#34;</p> <p>In Go if you import a package and don&#39;t use it you will get a compile error. This prevents you from bloating your binary with unneeded dependencies</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

460 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传