02.go-basic syntax-Package and visibility

Source: Internet
Author: User
This is a created article in which the information may have evolved or changed.

Basic syntax

Package

Basic structure

The basic structure of the package is as follows

//程序运行的入口是包 `main`。package main//导入包"fmt"和"math/rand"import ("fmt""math/rand")//或import std "fmt"//指定别名import "math"// 常量的定义const PI = 3.14// 全局变量的声明与赋值var name = "gopher"// 一般类型声明type newType int// 结构的声明type gopher struct{}// 接口的声明type golang interface{}// 函数的声明func add(x int,y int) int{return x + y}// 当两个或多个连续的函数命名参数是同一类型,则除了最后一个类型之外,其他都可以省略。func add2(x, y int) int{return x + y}// 由 main 函数作为程序入口点启动func main() {fmt.Println("My favorite number is", rand.Intn(10))    fmt.Println(add(42,13)}

Alias

You can specify an alias while importing a package

import std "fmt"

When defining an alias, you can call it by using the following method

std.Println("Hello world!你好,世界!")

Note that after you define an alias, you cannot call it by its original name.

Visibility rules

Go uses casing to determine whether constants, variables, types, etc. can be called externally

By convention, the first letter of the function name is private and uppercase is public

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.