golang 我正在努力学习去讨厌自己
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang 我正在努力学习去讨厌自己相关的知识,希望对你有一定的参考价值。
package main
import (
"fmt"
"strconv"
)
func main() {
fmt.Println(sum(3,255))
}
func sum(one, two uint8) (out uint16) {
var outBits [8]byte
sum, carry := halfAdder( getBit(one, 0), getBit(two, 0) )
if sum {
outBits[0] = '1'
} else {
outBits[0] = '0'
}
for i := 7; i >= 0; i-- {
sum, carry = fullAdder( getBit(one, i), getBit(two, i), carry )
if sum {
outBits[i] = '1'
} else {
outBits[i] = '0'
}
}
var carryChar string
if carry {
carryChar = "1"
} else {
carryChar = "0"
}
out64,_ := strconv.ParseUint(carryChar + string(outBits[:8]), 2, 16)
out = uint16(out64)
return
}
func halfAdder(one bool, two bool) (sum bool, carry bool) {
sum = one != two
carry = one && two
return
}
func fullAdder(one bool, two bool, carryIn bool) (sum bool, carryOut bool) {
xor := one != two
sum = xor != carryIn
and1 := one && two
and2 := xor && carryIn
carryOut = and1 || and2
return
}
func getBit(value uint8, pos int) bool {
bin := strconv.FormatUint(uint64(value), 2)
binLen := len(bin)
var bin8 [8]byte
for i := 0; i < (8-binLen); i++ {
bin8[i] = '0'
}
for i := (8-binLen); i < 8; i++ {
bin8[i] = bin[i-(8-binLen)]
}
return bin8[pos] == 49
}
golang 去并发测试(我学习去)
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println("Concurrency test")
concurrency := 5
workers := make(chan bool, concurrency)
// Ensure each worker is set to true (ready)
for i := 0; i < cap(workers); i++ {
select {
default:
workers <- true
}
}
// i is purely for output reasons
for i := 0; ; i++ {
select {
case <-workers:
output := make(chan string)
go func(w chan bool) {
defer func() {
w <- true
}()
output <- fmt.Sprintf("request %d", i)
time.Sleep(1000 * time.Millisecond)
}(workers)
fmt.Println(<-output)
}
}
}
以上是关于golang 我正在努力学习去讨厌自己的主要内容,如果未能解决你的问题,请参考以下文章
中兴程序员之死--也谈中年危机
Clojure 之美
New start-开始我的学习记录吧
golang 去并发测试(我学习去)
努力 奋斗
golang:exported function Script should have comment or be unexported