Go Language Renderer Package code Analysis

Source: Internet
Author: User
Tags email string uppercase character uppercase letter
This is a creation in Article, where the information may have evolved or changed.

Renderer is a simple, lightweight, responsive rendering package for the go language that supports responses of JSON, JSONP, XML, Hyaml, HTML, file, and so on. This package is a handy toolkit when developing a web app or restful API.

This article revolves around how to use it, drill down into code implementations, and try out the development routines of the Go Language pack.

Go Package basic structure

package pkgnameimport (    "fmt"    ...)const (    CONST1 typeX = xx    ...)var (    VAR1 typeX = xxx    ...)func Fn1() {}

In the go language, the package name and directory name remain consistent, and namespaces can be shared within the same package.

    • At the beginning of the package file, except for comments, the first line must be package pkgname, which declares the name of the bundle.
    • After the package declaration, you can import the packages and other external packages in the standard library.
    • You can then define package constants, package variables (exposure variables and non-exposed variables, which are written in case of initial capitalization).
    • Then define a custom type, function, or method.

Import statement

Import can introduce a package from a standard library, or you can introduce an external package. Once a package has been introduced into the go language, the namespace of the package must be used in the program, or the compilation error will tell you that a package was introduced but not used in the code.

Of course you will also have questions, if I need to introduce a package, but do not want to use what to do. This go language has a special symbol "_", put in front of the introduction of the package name, you can prevent compilation error. Why would this be considered? Because sometimes, we just want to introduce a package and then execute some initialization settings for this package. You then temporarily do not use any of the methods and variables of the package in your code.

import (    _ "gitHub.com/xxxxx/pkgname")

The above statement introduces the Pkgname namespace, but temporarily does not use the namespace in code. When this is introduced, the init () function is looked up in the Pkgname package and then executed before the main () function executes, which is useful for initializing before the package needs to be used.

The realization of exposure and non-exposure

In other programming languages, we have been exposed to modifiers such as private, protected, public, and so on. But in the go language there is absolutely no such, but the go language can be something out of the package, and some things do not expose, it uses the principle is very simple, that is, if the identifier starts with a lowercase letter, outside the package is not visible, and if the identifier begins with an uppercase character, is visible outside the package, accessible.

It is straightforward to expose variables and functions (methods), but if they are exposed structures, the situation is slightly more complicated. But essentially, the outside of the struct if the lowercase letter begins, the inner attribute starts with an uppercase letter. The outer package is not accessed directly, but if the external type is returned through a function or method, the external type can be accessed by: =, thus allowing access to its internal properties. Examples are as follows:

// package pkgnamepackage pkgnametype admin struct {    Name string    Email String}func Admin() *admin {    return &admin{        Name: "admin",        Email: "admin@email.com",    }}

So we can access the properties inside the admin struct directly from the following code in the external package:

admin := pkgname.Admin()fmt.Println(admin.Name, admin.Email)

Of course, in this case, you will need to know the structure of the admin and the name of the property included in advance.

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.