golang- note 2


Structure:
is a data type.

type Person struct {- type definition (position equivalent to int byte bool string ....) usually placed in the global position.
String name
Sex byte
Age int
}

Common variable definitions and initialization:

1. Initialization sequence: sequentially initializing the internal structure of a desired member.

var man Person = Person{"andy", 'm', 20}

2. Specify the member initialization:

man: = Person {name: " rose", age: 18} ---- uninitialized variable member, to take the default value corresponding to the data type

assignment and the use of simple variables:

Use. "" Index member variables.

var man3 Person
man3.name = "mike"
man3.sex = 'm'
man3.age = 99

Comparison of structure variables:

1. Compare: You can not use == and => <> = <= ...!

2. The same structure type (type member variable number consistent sequence) may be directly between the variable assignment.

Structures Address:

Address of the first element of the structure variable address == structures.

Parameter passing structure:
unSafe.Sizeof (variable name) -> this type of variable size of the memory space occupied by

the value of a structure variable copy, transfer. - almost no. Memory consumption and low efficiency.

Define and initialize pointer variables:

1. Initialization sequence: sequentially initializing the internal structure of a desired member.

var man *Person = &Person{"andy", 'm', 20}

2. new(Person)

p := new(Person)
p.name = "name"
p.age = 10


Index pointer becomes a member variable:

Use. "" Index member variables.

Man3 the Person var
man3.name = "Mike"
man3.sex = 'm'
man3.age = 99
structure Address:

Address of the first element structure pointer variable value == structure.

Structure pointer parameter transmission:
unSafe.Sizeof (Pointer): Regardless of the type of pointer, in a 64-bit operating system, the same size. Are 8 bytes! ! !

The structure variable address value, pass (pass by reference). - frequency of use is very high! ! !

Exercise:
definition of a structure, comprising members of the string, int, bool, [] string.

Definition of the structure "Common Variables" main function, is not initialized. Wrapper function initFunc, in the initialization function, main function Print View.


Structure pointer return value of function:

We can not return address local variables. - Local variables stored on the stack frame after the function call stack frame release. Address local variables are no longer protected by the system, at any time may be assigned to other programs.

You can return the value of the local variable.

String handling functions:

1. The specified string delimiter Split: Split

ret := strings.Split(str, " I")

2. Press the space bar to split the string: Fields

ret = strings.Fields(str)

3. End Analyzing string tag HasSuffix

flg := strings.HasSuffix("test.abc", ".mp3")

4. Analyzing string start mark HasPrefix

flg := strings.HasPrefix("test.abc", "tes.")

Open, create a file:

1. Create a file Create: create a file does not exist, the file exists, empty the contents of the file.

Parameters: name, the file open path: absolute path, relative path delimiter directory: /

2. Open the file Open: open read-only file. File does not exist, open failed.

Parameters: name, open the file path: absolute path, relative path

3. Open the file OpenFile: read-only, write-only, read-write mode to open the file. File does not exist, open failed.

Reference 1: name, open the file path: absolute path, relative path

Reference 2: Open the file permissions: O_RDONLY, O_WRONLY, O_RDWR

Reference 3: General pass 6

Write file:

Press string write: WriteString) (-> n write the number of characters

n, err := f.WriteString("123")

Carriage return line feed: windows: \ r \ n Linux : \ n

by location write:
the Seek (): write pointer position to modify the file.

Reference 1: Offset. Positive: Partial the end of the file, negative: skewed to the header

Reference 2: Offset starting position:

io.SeekStart: start of the file

io.SeekCurrent: Current file location

io.SeekEnd: End of file location

Return Value: pointer indicates the location to read and write the offset from the start of the file, the current file.

off, _: = f.Seek (-5 , io.SeekEnd)
Byte Write:
writeAt (): the development of an offset position in the file, write the [] byte, usually with the Seek ()

Reference 1: The data to be written

Reference 2: Offset

Returns: the number of bytes actually written.

n, _ = f.WriteAt([]byte("1111"), off)

Reading file:
row reading
1) create a Reader (reader with a buffer).

reader: = bufio.NewReader (open file pointer)

2) from the reader in the buffer, the read data specified length. Parameter depends on the data length dlime

buf, err: = reader.ReadBytes ( '\ n') read by row.

Judge reached the end of the file:! If err = nil && err == io.EOF to the end of the file.

End of file marker to read alone once acquired.

Buffer: an area of memory used to reduce the physical disk access operations. "Computing hardware and composition principle" - Machinery Industry Press.

Byte read and write files.

read ([] byte): reading the file in bytes

write ([] byte): Byte Byte

Directory operations:

Open Directory: OpenFile

Open Directory OpenFile: open read-only directory.

Reference 1: name, open the directory path: absolute path, relative path

Reference 2: Open the directory permissions: O_RDONLY

参 3: os.ModeDir

Return Value: Returns a pointer to the file can be read directory.

Read Directory: readdir

Prototype: func (n int) ([ ] FileInfo, error) {(f * File) Readdir

parameters: the number of directory entries to be opened. -1 table all

返回值: FileInfo :
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)
}

3 exercises, ideas analysis:

1. In accordance with user-specified directory, read-only open - reading exercises directory.

2. Locate the directory .txt, there may be multiple - directory to find a specific file type

3. Open one .txt file. Loop reads one line. reader: = bufio.NewReader, reader.ReadBytes ( '\ n') - read the contents of the file line exercises

4. The string line data, after the split, stores [] string. Split, Fields - string exercises

5. traverse [] string statistics "Love" the number of times the word appears. - map exercises

C:/itcast/test

Guess you like

Origin www.cnblogs.com/landv/p/10984058.html