01/02 Study Daily record

손진성·2022년 2월 2일
0

fallthrough

func main() {
	var word = "F"

	switch word {
	case "A", "B", "C", "D":
		fmt.Println("A or B or C or D")
	case "E", "F", "G":
		fmt.Println("E or F or G")
	case "H", "I", "J":
		fmt.Println("H or I or J")
        fallthrough
	default:
		fmt.Println("End Switch")
	}
}

-fallthrough makes activation of next step.
-So It prints H or I or J and End Switch

Binary Search

Main

 func main() {
	var values []int = []int{17, 28, 43, 67, 88, 92, 100}
	target := 43
	min := 0
	max := len(values)
 
	loc := binSearch(target, values...)
	fmt.Println(loc)

	locR := binSearchRec(min, max, target, values...)
	fmt.Println(locR)
}

Iteration

func binSearch(target int, i ...int) int {
	min := 0
	max := len(i)
	mid := 0

	for min < max {
		mid = int(math.Round(float64((min + max) / 2)))
		if i[mid] == target {
			return mid
		} else if i[mid] < target {
			min = mid + 1
		} else {
			max = mid - 1
		}
	}
	return -1
}

Recursion

func binSearchRec(min, max, target int, i ...int) int {
	mid := int(math.Round(float64((min + max) / 2)))
    	if min > max {
		return -1
	}
	if i[mid] == target {
		return mid
	} else if i[mid] < target {
		return binSearchRec(mid+1, max, target, i...)
	} else {
		return binSearchRec(min, mid-1, target, i...)
	}
}

Structure
Declare structure

type CMember struct {
	name string
	age int
	address string
	rank string
	clearance int
}

Assign values by sequence of structure

cm := CMember{"Kevin", 32, "Station Mars", "Sergeant", 5}

Declare cm by Name of Field(not in order)

	cm := CMember{
		name: "Kevin",
		clearance: 5,
		age: 32,
		rank: "Sergeant",
		address: "Station Mars",
	}

Declare cm by Name.Field(not in order)

	var cm CMember
	cm.name = "Kevin"
	cm.age = 32
	cm.address = "Station Mars"
	cm.rank = "Sergeant"
	cm.clearance = 5

Print result

{Kevin 32 Station Mars Sergeant 5}
  • It's very common to have pointers to structures in the code.

Structure in Slice

Declare Slice and add cm, values

	var crew []CMember
	crew = append(crew, cm, CMember{"jo", 32, "Station Mars", "Sergeant", 5})

Print result

[{Kevin 32 Station Mars Sergeant 5} {jo 32 Station Mars Sergeant 5}]

Structure in Map

Declare Map and add cm

	var m map[string]CMember
	m = make(map[string]CMember)
	m["Kevin"] = cm

Print result

map[Kevin:{Kevin 32 Station Mars Sergeant 5}]

Declare Map and add values with the structure

	m := map[string]CMember{
		"Kevin": CMember{name: "Kevin", address: "Station Mars"},
		"Jo":    CMember{name: "Jo", address: "Station Jupiter"},
	}

Print result

map[Jo:{Jo 0 Station Jupiter  0} Kevin:{Kevin 0 Station Mars  0}]

Add a structure in Map

m["Cisco"] = CMember{name: "Cisco", address: "Station Mars", clearance: 5}

Retrive

elem := m["Jo"]

Print result

{Jo 0 Station Jupiter  0}

Modification Test

	elem := m["Jo"]
	fmt.Println(elem)
	m["Jo"] = CMember{address: "Station Mars"}
	fmt.Println(elem)
	fmt.Println(m["Jo"])

Print result

{Jo 0 Station Jupiter  0}
{Jo 0 Station Jupiter  0}
{ 0 Station Mars  0}
  • elem does not have pointer of Map
  • Also tried to modify an address value, all values were updated as 0 except a declared value

Check a value and a existence

v, ok := m["Jo"]

Print result

{ 0 Station Mars  0} true

Delete a value in a map and check a value

delete(m, "Jo")
v, ok = m["Jo"]

Print result

{ 0   0} false

Print Map keys and values

for k, v := range m {
		fmt.Println("Key:", k, "Value", v)
	}

Print result

Key: Kevin Value {Kevin 0 Station Mars  0}
Key: Cisco Value {Cisco 0 Station Mars  5}

Method for cm

func (cm CMember) PrintSecurityClearance() {
	fmt.Println(cm.clearance)
}

func main() {
var cm CMember
	cm.name = "Kevin"
	cm.age = 32
	cm.address = "Station Mars"
	cm.rank = "Sergeant"
	cm.clearance = 5
   
   cm.PrintSecurityClearance()
}

Print result

5
  • A method is a special function that uses the fields of a structure to perform a specific function.
    -Instead of declaring a method within a structure, it is declared separately like a normal function.
  • Basically, methods are declared in the form of "func (parameter name structure name) method name() return type {". The parameter name is a structure variable name and is used like a parameter within a method.
  • Instead of entering 'function name', enter the method name after the structure name. We use the name to access the method in the body.
profile
Gopyther

0개의 댓글