My Go Learning Library

My Go Learning Library

  • A must-see must-see for beginners in Go language
  • A must-see must-see for beginners in Go language
  • A must-see must-see for beginners in Go language

It has been three or four days since I started learning the Go language. With the great encouragement of the Nuggets, I have continued to push the learning to one climax after another. A new problem encountered while studying in a scattered way is that I live in the mountains and have no broadband. I can only access the Internet through network traffic, and it is unstable. This requires an offline document. I will introduce my solution to you in detail below.

1. How do I write recently

When it comes to recent writing, I have to talk a lot about it. I used typora before, but I have to pay for it. The fee is a good thing. I support the author. If you use it, you must pay. Currently learning Go language, I use vscode, of course, it supports vscode first, without much to say, search markdown directly in the plug-in, download the markdown plug-in (if nothing else, just pick the first one, the full name of the plug-in: Markdown All in One), choose a popular plug-in to start writing, you can preview it when you want to preview it, the effect is great, and it supports shortcut keys, which is very convenient. The common operations are recorded as follows:

markdown.jpg

  • Bold shortcut: Ctrl/Cmd + B Toggle bold
  • Ctrl/Cmd + I Toggle italic
  • Alt+S (on Windows) Toggle strikethrough1
  • Ctrl + Shift + ] Toggle heading (uplevel)
  • Ctrl + Shift + [ Toggle heading (downlevel)
  • Ctrl/Cmd + M Toggle math environment
  • Alt + C Check/Uncheck task list item
  • Ctrl/Cmd + Shift + V Toggle preview
  • Ctrl/Cmd + K V Toggle preview to side

Wherever you learn, you write where you are, and that's how learning is, especially self-study. The above are some commonly used shortcut keys, it will be much more convenient to use shortcut keys.

2. What are my recent study materials?

For the recent study, my first choice is the open source study books** "The Way to Go", "Go Beginner's Guide", Chinese official name "Go Beginner's Guide"**

Book address: github.com/unknwon/the…

The author is an avid fan of the Go language, recording the video tutorial "Go Programming Basics". The author is too humble, a particularly powerful person, I am learning from his github. At the same time, according to the author, the translated version has been authorized by the original author (Ivo Balbaert), and expressed his support for the development of open source!

In particular, the author's githu id is called unknown , which is too modest.

way-to-go.jpg

3. Build a local learning library

This open source book is written in markdown, which is very good, but it is very troublesome to use, that is, the file names are all numbers, and it is very troublesome to read, especially to jump. What should I do? I want to read it offline and locally.

Speaking of which, I opened the author's github and found a treasure open source repository github.com/asoul-sig/a… , this is a web multi-document system written in Go language, featuring markdown, just to meet my needs.

Let's take a look at how I convert e-books into web local novel reading mode.

  • 1. Download the book warehouse (see the introduction of 2)
  • 2. Download the asoul document repository
  • 3. Compile the asoul document software
  • 4. Create a new docs directory under asoul
  • 5. Create a new toc.ini document
  • 6. Add titles to markdown documents in batches

asoul.jpg

4. Code section

When it comes to the creation of toc.ini documents and adding titles to markdown documents, manual parties are of course impossible (too tiring). It can be done with a few lines of python code. Let's learn it below.

import os
from pathlib import Path
import glob


def getname(filename):
    return Path(filename).stem.strip('.md')


def gettitle(filename):
    with open(filename, 'r', encoding='utf-8') as f:
        title = f.readline()
        title = title.strip("#").strip(' ').strip("\n")
    return title


# ---
# title: 配置反向代理
# ---
def rewrite(filename):
    title = gettitle(filename)
    with open(filename, "r+", encoding='utf-8') as f:
        old = f.read()
        f.seek(0)
        f.write('---\n')
        f.write("title: " + title + '\n')
        f.write('---\n\n')
        f.write(old)


def replace_img(filename):
    title = gettitle(filename)
    with open(filename, "r+", encoding='utf-8') as f:
        old = f.read()
        old = old.replace(r'![](images/', r'![](../../assets/')
        print(type(old))
        print(old)
        f.seek(0)
        f.write(old)


def rewrite_dir_imgs(basedir):
    for file in glob.glob(basedir):
        replace_img(file)


def rewrite_dir(basedir):
    for file in glob.glob(basedir):
        rewrite(file)


def write_toc(basedir, globdir):
    with open(os.path.join(basedir, 'toc.ini'), 'w', encoding='utf-8') as f:
        for file in glob.glob(globdir):
            f.write('-: ' + getname(file) + '\n')
    print("toc.ini written finished.")


if __name__ == "__main__":
    basedir = r"C:\Users\livingbody\Goworkspace\src\asouldocs-main\docs\zh-CN\eBook"
    globdir = r"C:\Users\livingbody\Goworkspace\src\asouldocs-main\docs\zh-CN\eBook\*.md"
    # 转写图片地址
    rewrite_dir_imgs(globdir)
    # 添加标题
    rewrite_dir(globdir)
    # 写toc.ini文件
    write_toc(basedir, globdir)

The code part is so much, it's very simple. Then execute it on the command line

.\asouldocs.exe web

Start the Go e-book, and you can read a book like a novel.

ebook.jpg

This article is participating in the 18th issue of technical topics - chatting about the Go language framework

Guess you like

Origin juejin.im/post/7120619735963664415