Read the Go Language source code 1

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Before probably said Bison and Yacc's grammar, then actually go also uses the Bison to realize, below say how to read the Go Language source code. To see the code first to find the code entry, how to find it? You might think of grep main first yes, generally this is a uncontroversial approach, but for the go language, this approach is not right. What do you say? We'll use GDB to debug and we'll see all the entries in the Go Language program in the main function of the SRC/LIB9/MAIN.C.

#include 
  
   
    
   #define NOPLAN9DEFINES#include 
   
    
     
    extern void p9main(int, char**);intmain(int argc, char **argv){    p9main(argc, argv);    exits("main");    return 99; }
   
    
  
   

From the go/include/libc.h, we can see a sentence like this.

#ifndef NOPLAN9DEFINES#define main    p9main#endif

That is, in the Go Language source code, the main function is renamed, all the functions of the entry into the SRC/LIB9/MINA.C, but also the actual entrance of the respective code, so that the entrance is the main of the sentence, right or wrong.

Well, let's talk about the code structure of the Go language.

.|   --bin//my own definition of the installation directory |--doc//related documents, Godoc is to use this directory |   |--codelab|   |   '--wiki|   |--codewalk|   |--devel|   |--gopher|   |--progs|       '--talks|   '--io2010|--include//Some global definition files |--lib//Some LIB9 related global libraries |   |--codereview|   '--godoc|--misc//some Tools |   |--arm|   |--bash|   |--bbedit|   |--cgo|   |   |--gmp|   |   |--life|   |   '--stdio|   |--dashboard|   |   '--godashboard|   |--emacs|   |--kate|   |--nacl|   |--vim|   |   |--ftdetect|   |   '--syntax|   '--xcode|--pkg//compiled Go Library |       '--linux_amd64|       |--archive|       |--compress|       |--container|       |--crypto|       |--debug|       |--encoding|       |--exp|       |--go|       |--hash|       |--http|       |--image|       |--io|       |--os|       |--rpc|       |--runtime|   '--testing|--src|   |--Cmd//go language-related Programs |   |   |--5a|   |   |--5c|   |   |--5g|   |   |--5l|   |   |--6A//AMD64 Assembler |   |   |--C Compiler for 6C//AMD64 |   |   Compiler for |--6g//amd64 |   |   |--6L//AMD64 's Linker |   |  |--8a| |   |--8c|   |   |--8g|   |   |--8l|   |   |--cc|   |   |--cgo|   |   |--cov|   |   |--ebnflint|   |   |--gc|   |   |--godefs|   |   |--godoc|   |   |--gofmt|   |   |--goinstall|   |   |--gopack|   |   |--gotest|   |   |--goyacc|   |   |--hgpatch|   |   |--ld|   |   |--nm|   |   '--prof|   |--LIB9//LIB9 Related Galleries |   |   |--fmt|   |   '--utf|   |--libbio//Global Library |   |--Libcgo//cgo's Library |   |--libmach//Library |       '--Pkg//go Library |       |--archive|       |--asn1|       |--big|       |--bufio|       |--bytes|       |--cmath|       |--compress|       |--container|       |--crypto|       |--debug|       |--ebnf|       |--encoding|       |--exec|       |--exp|       |--expvar|       |--flag|       |--fmt|       |--go|       |--gob|       |--hash|       |--html|       |--http|       |--image|       |--io|       |--json|       |--log|       |--math|       |--mime|       |--net|       |--netchan|       |--nntp|       |--once|       |--os|     |--patch|  |--path|       |--rand|       |--reflect|       |--regexp|       |--rpc|       |--runtime|       |--scanner|       |--sort|       |--strconv|       |--strings|       |--sync|       |--syscall|       |--syslog|       |--tabwriter|       |--template|       |--testing|       |--time|       |--unicode|       |--unsafe|       |--utf16|       |--utf8|       |--websocket|   '---XML '--test//test |--bench |--Bugs |--Chan |--fixedbugs |   |--Bug083.dir |   |--Bug088.dir |   |--Bug106.dir |   |--Bug133.dir |   |--Bug160.dir |   |--Bug191.dir |   |--Bug222.dir |   |--Bug226.dir |   |--Bug248.dir | '--bug282.dir |--garbage |--interface |--Ken |--nilptr '--syntax

Let's start by looking at a go language Hello World file Hello.go

package mainfunc main(){    println("Hello World");}

Then the file will be compiled to link to an executable file, then execute the following command:

6g hello.o6l hello.6 -o hello

Obviously the key is the 6g and 6l two programs, then we start from 6g

First Gdb-tui Open the Tui interface GDB program

Then enter file 6g to load the 6g program, you can see

/HOME/HOPING/GO/SRC/LIB9/MAIN.C, and then enter start, step enters the main function of the/HOME/HOPING/GO/SRC/CMD/GC/LEX.C

So all we have to do is look at this file and we'll probably see the program's execution flow. The next time we don't need gdb, we change VI and ctags.

Execute ctags-r * in Go directory to generate tag, and then open lex.c with VI to enter the main function, in fact, even if you look at this function will not have much to gain.

As we said earlier, 6g is bison, so obviously we need to find the Bison file and the Yylex function. Since the entrance is in the GC folder, guess y file is also in the GC folder, so you can see go.y after entering LS *.y. The input of GO.Y is provided by the Yylex function. So first, let's take a look at the Yylex function, execute vim-t yylex under the Go folder, and select the GC folder.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.