Interface Type

Interface names conventions

By convention, one-method interfaces are named by the method name plus an -er suffix.

type Greetinger interface {
    Greeting() string

Define an interface type

It start with the keyword “type” follow by the interface name (ended with -er). Begin of a block of code symbol “{“, the function name (eg. Greeting() function and return a string ). One or more than one function can be added here then is often called a method set and ending of a block of code “}”.


type FileInfo interface {
        Name() string       // base name of the file
        Size() int64        // length in bytes for regular files; system-dependent for others
        Mode() FileMode     // file mode bits
        ModTime() time.Time // modification time
        IsDir() bool        // abbreviation for Mode().IsDir()
        Sys() interface{}   // underlying data source (can return nil)

In the above OS package, has FileInfo interface contains the following method sets

  • Name() string
  • Size() int64
  • Mode() FileMode
  • ModTime() time.Time
  • IsDir() bool
  • Sys() interface{}

Implementing an Interface


Create a Module


Definition of GO Package

Go programs are organized into packages. A package is a collection of source files in the same directory that are compiled together. Functions, types, variables, and constants defined in one source file are visible to all other source files within the same package.

src (folder)


Variadic Function

A variadic function accept varying numbers of arguments.

To declare a variadic function, the type of of the final parameter is preceded by an ellipsis, “…”

func sum(vals int {}

…int ellipsis defined a function can be called with any number of arguments of this type

vals is []int slice, can be access this slice using index or for..range

Functions_Closures Language

Methods with Struct

type Point struct {
     x  float64
     y  float64

The Point struct type has two fields, x and y of float64

func (p Point) DistToOrgin()  {
       t := math.Pow(p.x, 2) + math.Pow(p.y, 2)​
       return math.Sqrt(t)​

A method is defined with use of the keyword “func” just like a definition of the function. Just after func keyword, the defined fields of struct used in the body of code of the function as a receiver. It takes a variable of struct type “p Point” as a receiver (input) to the method name DistToOrgin().

Functions_Closures Language

Function with a function return.

func foo() func() int { }

A function name foo() without parameters/arguments return anonymous function without parameter/arguments return an int type. Empty body

1 func foo()  func() int {
2      return func() int {
3            return 1
4      }
6 }

In the line 2, the return for calling func foo() and follow up with the code of anonmoys function func() with return int type signature. In this body of anonymous func() on line 3 the code return 1.

So when a func foo() called, the body code of func foo() executed instead of just return the results. The results of func foo() pass into anonymous function to be process and then return the results.

Language My Go Tutorial

Interface with abstract of methods

Defining an Interface

type Interface_Name interface {
    Method_Name(Input) (Outputs)

Keyword : type, interface

Interface_name user provide a descriptive name

Within the { } curly bracket, method sets are a list of method_name has been defined method name with parameters, return. No implementation details.

type Speaker interface {
   Speak() string

type FileInfo
A FileInfo describes a file and is returned by Stat and Lstat.

type FileInfo interface {
    Name() string       // base name of the file
    Size() int64        // length in bytes for regular files; system-dependent for others
    Mode() FileMode     // file mode bits
    ModTime() time.Time // modification time
    IsDir() bool        // abbreviation for Mode().IsDir()
    Sys() interface{}   // underlying data source (can return nil)

the FileInfo interface has the following method sets:

Name() string // return a string

Size() int64 // return int64

Mode() FileMode // return FileMode type

ModTime() time.Time

IsDir() bool // return a bool type

Sys() interface{} // return an interface type


Language Specification: Map Types

Map types
A map is an unordered group of elements of one type, called the element type, indexed by a set of unique keys of another type, called the key type. The value of an uninitialized map is nil.

MapType = “map” “[” KeyType “]” ElementType .
KeyType = Type .
The comparison operators == and != must be fully defined for operands of the key type; thus the key type must not be a function, map, or slice. If the key type is an interface type, these comparison operators must be defined for the dynamic key values; failure will cause a run-time panic.

map[*T]struct{ x, y float64 }
The number of map elements is called its length. For a map m, it can be discovered using the built-in function len and may change during execution. Elements may be added during execution using assignments and retrieved with index expressions; they may be removed with the delete built-in function.

A new, empty map value is made using the built-in function make, which takes the map type and an optional capacity hint as arguments:

make(map[string]int, 100)
The initial capacity does not bound its size: maps grow to accommodate the number of items stored in them, with the exception of nil maps. A nil map is equivalent to an empty map except that no elements may be added.

Language My Go Tutorial Struct Type

Struct type

Struct types

A struct is a sequence of named elements, called fields, each of which has a name and a type. Field names may be specified explicitly (IdentifierList) or implicitly (EmbeddedField). Within a struct, non-blank field names must be unique.

StructType    = "struct" "{" { FieldDecl ";" } "}" .
FieldDecl     = (IdentifierList Type | EmbeddedField) [ Tag ] .
EmbeddedField = [ "*" ] TypeName .
Tag           = string_lit .
// Defined new Struct Type NOT fields yet
// Go language keyword "type" and "struct" plus {}
type Person struct {
// Field Description :- FieldDec1
// FieldDec1 :- IdentifierList Type
type Person struct {
    name string
    age int

Go Modules

A module is a collection of Go packages stored in a file tree with a go.mod file at its root.

The go.mod file defines the module’s module path, which is also the import path used for the root directory, and its dependency requirements, which are the other modules needed for a successful build.

From Go 1.13 onwards, this step “set GO111MODULE=on” won’t be required since Go modules will be enabled by default.