当开发人员将他们的软件作为开源发布时,这表明他们既值得信赖又慷慨。开源软件(Open-source software)是帮助我开始成为一名自学成才的程序员的主要因素之一。
对于开源项目,开发人员必须公开其源代码,以便任何有兴趣的人都可以检查并从中学习。重要的是,它还表明开发人员没有恶意,因为开源代码允许用户编译自己的二进制文件,而不是使用任何提供的二进制文件。
如果您遇到过两种软件并且无法确定使用哪个软件,请检查一个是否是开源的,而另一个不是。如果是这样,您的选择应该很明确。
但是,如果您想学习甚至添加到某个软件的源代码中,这也是一种选择。为此,您需要知道如何查看源代码。在本文中,让我们讨论如何做到这一点。
如何找到源代码(How to Find the Source Code)
一旦您找到了一个您想要查看其源代码的开源软件,第一步就是实际了解该项目的源代码是如何提供的。
在大多数情况下,源代码将托管在网络上最大的版本控制平台GitHub 上(GitHub)。
什么是 GitHub?(What Is GitHub?)
GitHub成立于 2008 年,2018 年被微软(Microsoft)收购。它是一个全球软件开发平台,提供Git的所有源代码管理功能,以及增强其协作能力和版本控制功能的自身功能。
通常情况下,开发人员会将他们的开源代码托管在GitHub 上(GitHub)。您可能听说过一些在GitHub 上(GitHub)提供源代码的软件示例,例如LibreOffice、GIMP和VLC 媒体播放器(VLC media player)。
使用GitHub 的搜索功能(GitHub’s search feature),您应该能够找到和查看几乎每个主要的开源软件项目的源代码。
如何查看源代码(How to View the Source Code)
找到托管软件源代码的GitHub存储库后,您会发现所有内容都组织在一个基本目录树中。对于文件,GitHub使查看代码变得非常容易。
作为一个开发平台,GitHub通过其 Web 界面巧妙地格式化源代码。几乎(Almost)所有有信誉的项目都会包含一个README.md,这是一个降价文件,显示有关存储库及其结构的有用信息。当您导航到存储库时,向下滚动,您将看到该文件的内容。
存储库的README.md很重要,因为它经常将用户指向他们可能感兴趣的源代码的方向。存储库可以充满各种代码和其他数据,其中一些对您完全无用,所以这个文件就是你的路线图。
一旦您确定了您感兴趣的代码在存储库中的位置,您所要做的就是单击存储库的目录树以找到您想要去的地方。
单击文件将以正确的语法高亮显示。
对于快速检查,GitHub 的 Web 界面非常棒。但是,对于更复杂的源代码,您可能希望同时或长时间查看多个文件。在这种情况下,我们建议通过文本编辑器(text editor)(例如 Sublime Text)下载和查看源代码。
下载源代码(Downloading The Source Code)
从GitHub(GitHub)下载存储库只需单击两次。
在每个存储库的顶部,在显示提交、分支、包、发布和贡献者总数的行下方,您会看到一个标有克隆或下载(Clone or download)的绿色按钮。单击它并选择下载 ZIP(Download ZIP)。
默认情况下,这将开始将当前存储库的主分支下载为ZIP文件。完成后,您只需将存档解压缩到计算机上的本地文件夹即可。然后,使用文本编辑器,您可以更快地打开存储库的任何文件,而无需浏览器。
如果您不是经验丰富的编码人员,GitHub一开始可能会有些混乱。如果你只是把它想象成一个源代码的开放目录,在顶层有一个自述文件,它并不会太吓人。使用GitHub(GitHub)查看源代码很简单,无论是在本地还是通过其 Web 界面。
How To View The Source Code Of An Open-Source Piece Of Software
When deνеlopers release their software as open-source, it shows that they are both trustworthy аnd generous. Open-source software was one of the main things that personally helped me get started as a self-taught coder.
For a project to be open-source, the developer has to make its source code publicly available so that anyone interested can inspect and learn from it. Importantly, it also shows that the developer has no nefarious intentions, as open-source code allows users to compile their own binaries rather than use any provided.
If you ever come across two pieces of software and you can’t determine which to use, check to see if one is open-source and the other is not. If so, your choice should be clear.
However, if you want to learn from or even add on to a piece of software’s source code, that’s also an option. To do so, you’ll need to know how to view the source code. In this article, let’s talk about how you can do just that.
How to Find the Source Code
Once you’ve found a piece of open-source software that you would like to view the source code of, the first step is to actually find out how the project’s source code is made available.
In the majority of cases, the source code will be hosted on the web’s largest version control platform, GitHub.
What Is GitHub?
GitHub was founded in 2008 and purchased by Microsoft in 2018. It’s a global software development platform that offers all of the source code management functionality of Git, along with its own features that enhance its collaboration capabilities and version control features.
More often than not, developers will host their open-source code on GitHub. A few examples of software that you’ve probably heard of which have source code available on GitHub are LibreOffice, GIMP, and VLC media player.
Using GitHub’s search feature, you should be able to find and view the source code for practically every major software project that’s open-source.
How to View the Source Code
Once you’ve found the GitHub repository that hosts the software’s source code, you’ll find that everything is organized in a basic directory tree. For files, GitHub makes viewing code very easy.
As a development platform, GitHub neatly formats source code through its web interface. Almost all reputable projects will include a README.md, which is a markdown file that displays helpful information about the repository and its structure. When you navigate to a repository, scroll down and you’ll see the contents of this file.
The repository’s README.md is important to check out because it often points users in the direction of the source code that they may be interested in. A repository can be filled with all sorts of code and other data, some of it completely useless to you, so this file is your roadmap.
Once you figure out where in the repository the code that you’re interested in is located, all you have to do is click through the repository’s directory tree to get where you want to go.
Clicking on a file will display it with the proper syntax highlighting.
For quick inspecting, GitHub’s web interface is great. However, for more complicated source code, you may want to view multiple files at the same time or over a long period of time. In this case, we recommend downloading and viewing the source code through a text editor, such as Sublime Text.
Downloading The Source Code
Downloading a repository from GitHub takes just two clicks.
At the top of each repository, beneath the row displaying the total number of commits, branches, packages, releases, and contributors, you’ll see a green button labeled Clone or download. Click on it and select Download ZIP.
By default, this will begin downloading the current repository’s master branch as a ZIP file. When complete, all you need to do is extract the archive to a local folder on your computer. Then, using a text editor, you can open any of the repository’s files in a much quicker way that doesn’t require a browser.
If you aren’t an experienced coder, GitHub can be a little confusing at first. If you just think of it as an open directory of source code, with a readme at the top level, it’s not too intimidating. Viewing source code using GitHub is simple, both locally and through its web interface.