• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang fmt.Sscan函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中fmt.Sscan函数的典型用法代码示例。如果您正苦于以下问题:Golang Sscan函数的具体用法?Golang Sscan怎么用?Golang Sscan使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了Sscan函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: main

func main() {
	var n int
	data, err := os.Open(os.Args[1])
	if err != nil {
		log.Fatal(err)
	}
	defer data.Close()
	scanner := bufio.NewScanner(data)
	for scanner.Scan() {
		t := strings.Split(scanner.Text(), ";")
		u := strings.Split(t[0], ",")
		fmt.Sscan(t[1], &n)
		v, vk := make(map[int]bool, len(u)), make([]int, len(u))
		for i := range u {
			var w int
			fmt.Sscan(u[i], &w)
			v[w], vk = true, append(vk, w)
		}
		f := false
		for _, i := range vk {
			if 2*i < n && v[n-i] {
				if f {
					fmt.Print(";")
				}
				fmt.Print(i, ",", n-i)
				f = true
			}
		}
		if !f {
			fmt.Print("NULL")
		}
		fmt.Println()
	}
}
开发者ID:oorja,项目名称:ce-challenges,代码行数:34,代码来源:number_pairs.go


示例2: sLane

func sLane(in string) string {
	var n, t int
	_, err := fmt.Sscan(in, &n, &t)
	if err != nil {
		return ""
	}
	fmt.Printf("n = %d, t = %d\n", n, t)
	w := make([]int, n)
	for i := 0; i < n; i += 2 {
		var f int
		_, err := fmt.Sscan(in[i+3:], &f)
		if err != nil {
			return ""
		}
		w = append(w, f)
	}

	v := make(TestCases, t)
	for z := 0; z < t; z++ {
		var tc TestCase
		_, err := fmt.Sscan(in[z+(n*2)+2:], &tc.Entry, &tc.Exit)
		if err != nil {
			return ""
		}
		v[z] = tc
	}
	fmt.Printf("%#v\n", v)

	for z := 0; z < len(v); z++ {

	}
	return ""
}
开发者ID:JonPulfer,项目名称:hackerrank,代码行数:33,代码来源:servicelane.go


示例3: intersect

func intersect(s, t string) string {
	var (
		a    int
		x, y []int
		r    []string
	)
	u, v := strings.Split(s, ","), strings.Split(t, ",")
	for _, i := range u {
		fmt.Sscan(i, &a)
		x = append(x, a)
	}
	for _, i := range v {
		fmt.Sscan(i, &a)
		y = append(y, a)
	}
	for i, j := 0, 0; i < len(u) && j < len(v); {
		if x[i] == y[j] {
			r = append(r, fmt.Sprint(x[i]))
			i++
			j++
		} else if x[i] > y[j] {
			j++
		} else {
			i++
		}
	}
	return strings.Join(r, ",")
}
开发者ID:nikai3d,项目名称:ce-challenges,代码行数:28,代码来源:intersection.go


示例4: main

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	// width: the number of cells on the X axis
	var width int
	scanner.Scan()
	fmt.Sscan(scanner.Text(), &width)

	// height: the number of cells on the Y axis
	var height int
	scanner.Scan()
	fmt.Sscan(scanner.Text(), &height)

	node := make([][]bool, height) //slice for nodes' rows

	for i := 0; i < height; i++ {
		node[i] = make([]bool, width) //slice - nodes' row
	}

	for i := 0; i < height; i++ { //find nodes
		scanner.Scan()
		line := scanner.Text() // width characters, each either 0 or .
		for j := 0; j < len(line); j++ {
			if string(line[j]) == "0" {
				node[i][j] = true
			}
		}
	}

	for i := 0; i < height; i++ {
		for j := 0; j < width; j++ {
			if node[i][j] { //is there a node?
				res := strconv.Itoa(j) + " " + strconv.Itoa(i) + " " //start of response
				for k := j + 1; k < width; k++ {
					if node[i][k] { //is there a node?
						res += strconv.Itoa(k) + " " + strconv.Itoa(i) + " " //inner response
						break
					}
				}
				if len(res) < 7 { //was node found?
					res += "-1 -1 "
				}
				for k := i + 1; k < height; k++ {
					if node[k][j] { //is there a node?
						res += strconv.Itoa(j) + " " + strconv.Itoa(k) //end response
						break
					}
				}
				if len(res) < 11 { //was node found?
					res += "-1 -1"
				}
				fmt.Println(res) //output response
			}
		}
	}

	// fmt.Fprintln(os.Stderr, "Debug messages...")

	//fmt.Println("0 0 1 0 0 1") // Three coordinates: a node, its right neighbor, its bottom neighbor
}
开发者ID:Renerte,项目名称:CodinGameSolutions,代码行数:60,代码来源:apu_init.go


示例5: main

func main() {
	B := new(big.Int).Exp(big.NewInt(2), big.NewInt(20), big.NewInt(0))

	p := new(big.Int)
	g := new(big.Int)
	h := new(big.Int)

	_, err := fmt.Sscan("13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084171", p)
	_, err = fmt.Sscan("11717829880366207009516117596335367088558084999998952205599979459063929499736583746670572176471460312928594829675428279466566527115212748467589894601965568", g)
	_, err = fmt.Sscan("3239475104050450443565264378728065788649097520952449527834792452971981976143292558073856937958553180532878928001494706097394108577585732452307673444020333", h)
	if err != nil {
		fmt.Println(err)
	}

	// Create a map that uses the calculations for x1's as keys
	hashTable := make(map[string]int64, B.Int64())

	var i int64
	for i = 0; i < B.Int64(); i++ {
		hashTable[computeX1(p, g, h, big.NewInt(i)).String()] = i
	}

	// Calculate the x0's and check if they are part of the hash table
	for i = 0; i < B.Int64(); i++ {
		x0Res := computeX0(p, g, B, big.NewInt(i))

		x1, found := hashTable[x0Res.String()]
		if found {
			x := B.Mul(B, big.NewInt(i))
			x.Add(x, big.NewInt(x1))
			fmt.Println("Solution:", x)
			return
		}
	}
}
开发者ID:MariaRigaki,项目名称:golang-stuff,代码行数:35,代码来源:week5.go


示例6: main

func main() {
	data, err := os.Open(os.Args[1])
	if err != nil {
		log.Fatal(err)
	}
	defer data.Close()
	scanner := bufio.NewScanner(data)
	for scanner.Scan() {
		var n int
		fmt.Sscan(scanner.Text(), &n)
		b := make([]int, n)
		scanner.Scan()
		s := strings.Split(scanner.Text(), ",")
		fmt.Sscan(s[0], &b[0])
		for i := 1; i < n; i++ {
			var v int
			fmt.Sscan(s[i], &v)
			b[i] = b[i-1] + v
		}
		for i := 1; i < n; i++ {
			scanner.Scan()
			s = strings.Split(scanner.Text(), ",")
			var v int
			fmt.Sscan(s[0], &v)
			b[0] += v
			for j := 1; j < n; j++ {
				var v int
				fmt.Sscan(s[j], &v)
				b[j] = min(b[j], b[j-1]) + v
			}
		}
		fmt.Println(b[n-1])
	}
}
开发者ID:oorja,项目名称:ce-challenges,代码行数:34,代码来源:min_path_sum.go


示例7: main

func main() {
	data, err := os.Open(os.Args[1])
	if err != nil {
		log.Fatal(err)
	}
	defer data.Close()
	scanner := bufio.NewScanner(data)
	for scanner.Scan() {
		s := strings.Split(scanner.Text(), ";")
		t := strings.Fields(s[1])
		var m, n, c int
		fmt.Sscan(s[0], &n)
		u := make([]int, len(t))
		for ix, i := range t {
			fmt.Sscan(i, &u[ix])
		}
		for i := 0; i < n; i++ {
			c += u[i]
		}
		if c > m {
			m = c
		}
		for len(u) > n {
			c = c - u[0] + u[n]
			if c > m {
				m = c
			}
			u = u[1:]
		}
		fmt.Println(m)
	}
}
开发者ID:oorja,项目名称:ce-challenges,代码行数:32,代码来源:max_range_sum.go


示例8: GetEnvs

// Convert and validate the GOAGAIN_FD, GOAGAIN_NAME, and GOAGAIN_PPID
// environment variables.  If all three are present and in order, this
// is a child process that may pick up where the parent left off.
func GetEnvs() (l *net.TCPListener, ppid int, err error) {
	var fd uintptr
	_, err = fmt.Sscan(os.Getenv("GOAGAIN_FD"), &fd)
	if nil != err {
		return
	}
	var i net.Listener
	i, err = net.FileListener(os.NewFile(fd, os.Getenv("GOAGAIN_NAME")))
	if nil != err {
		return
	}
	l = i.(*net.TCPListener)
	if err = syscall.Close(int(fd)); nil != err {
		return
	}
	_, err = fmt.Sscan(os.Getenv("GOAGAIN_PPID"), &ppid)
	if nil != err {
		return
	}
	if syscall.Getppid() != ppid {
		err = errors.New(fmt.Sprintf(
			"GOAGAIN_PPID is %d but parent is %d\n",
			ppid,
			syscall.Getppid(),
		))
		return
	}
	return
}
开发者ID:brunoqc,项目名称:goagain,代码行数:32,代码来源:goagain.go


示例9: compareSpamSum

func compareSpamSum(left, right string) int {
	var leftSum, rightSum spamsum.SpamSum
	fmt.Sscan(left, &leftSum)
	fmt.Sscan(right, &rightSum)
	score := leftSum.Compare(rightSum)
	return int(score)
}
开发者ID:postfix,项目名称:spamsum,代码行数:7,代码来源:from_git_repo.go


示例10: TestVoteBox

func TestVoteBox(t *testing.T) {
	score := c.NewScore(69)
	server := hm.NewMock(map[string]hm.Responder{
		"/v": hm.FuncResponder(func(c *hm.Context) hm.Response {
			var vote, lastVote int
			// get values from the query parameters
			fmt.Sscan(c.Request.URL.Query().Get("vote"), &vote)
			fmt.Sscan(c.Request.URL.Query().Get("lastvote"), &lastVote)

			score.UserVote(vote, lastVote)

			return hm.NewOKResponse(fmt.Sprint(score.Score))
		}),
	})

	fntest.NewDummyTestApp("/", server)

	votebox := &VoteBoxModel{}
	votebox.VoteUrl = "/v"
	votebox.Vote = score

	// Start testing
	server.Wait(func() { votebox.DoVote(1) }, 1)
	require.Equal(t, votebox.Vote.Score, 70)
	server.Wait(func() { votebox.DoVote(-1) }, 1)
	require.Equal(t, votebox.Vote.Score, 68)
	server.Wait(func() { votebox.DoVote(-1) }, 1)
	require.Equal(t, votebox.Vote.Score, 69)
}
开发者ID:phaikawl,项目名称:wadereddi,代码行数:29,代码来源:client_test.go


示例11: Recover

// Recover from a seamless binary upgrade and use an already
// existing listener to take over the connections
func Recover() (l net.Listener, ppid int, err error) {
	var fd uintptr
	_, err = fmt.Sscan(os.Getenv("OLD_FD"), &fd)
	if err != nil {
		return
	}
	var i net.Listener
	i, err = net.FileListener(os.NewFile(fd, os.Getenv("OLD_NAME")))
	if err != nil {
		return
	}
	switch i.(type) {
	case *net.TCPListener:
		l = i.(*net.TCPListener)
	case *net.UnixListener:
		l = i.(*net.UnixListener)
	default:
		err = errors.New(fmt.Sprintf(
			"file descriptor is %T not *net.TCPListener or *net.UnixListener", i))
		return
	}
	if err = syscall.Close(int(fd)); err != nil {
		return
	}
	_, err = fmt.Sscan(os.Getenv("OLD_PPID"), &ppid)
	if err != nil {
		return
	}
	return
}
开发者ID:lu-zero,项目名称:mirrorbits,代码行数:32,代码来源:process.go


示例12: Load

func (this *BoolOptions) Load() {
	file, err := os.Open("settings.txt")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer file.Close()

	v := reflect.ValueOf(this).Elem()
	var n int

	for err == nil {
		var key, value string
		n, err = fmt.Fscanf(file, "%s %s\n", &key, &value)
		if n == 0 {
			return
		}

		fieldValue := v.FieldByName(key)
		switch fieldValue.Kind() {
		case reflect.Bool:
			var b bool
			fmt.Sscan(value, &b)
			fieldValue.SetBool(b)
		case reflect.Float32, reflect.Float64:
			var f float64
			fmt.Sscan(value, &f)
			fieldValue.SetFloat(f)
		}
	}
}
开发者ID:krux02,项目名称:turnt-octo-wallhack,代码行数:31,代码来源:Settings.go


示例13: CalculatePdpPrep

func (d *DatasourceDerive) CalculatePdpPrep(newValue string, interval float64) (float64, error) {
	if float64(d.Heartbeat) < interval {
		d.LastValue = Undefined
	}

	rate := math.NaN()
	newPdp := math.NaN()
	if newValue != Undefined && float64(d.Heartbeat) >= interval {
		newInt := new(big.Int)
		_, err := fmt.Sscan(newValue, newInt)
		if err != nil {
			return math.NaN(), errors.Errorf("not a simple signed integer: %s", newValue)
		}
		if d.LastValue != "U" {
			prevInt := new(big.Int)
			_, err := fmt.Sscan(d.LastValue, prevInt)
			if err != nil {
				return math.NaN(), errors.Wrap(err, 0)
			}
			diff := new(big.Int)
			diff.Sub(newInt, prevInt)

			newPdp = float64(diff.Uint64())
			rate = newPdp / interval
		}
	}

	if !d.checkRateBounds(rate) {
		newPdp = math.NaN()
	}

	d.LastValue = newValue

	return newPdp, nil
}
开发者ID:untoldwind,项目名称:gorrd,代码行数:35,代码来源:datasource_derive.go


示例14: blockexplorerBlockDataHandler

// Handles the call to get many data points on blocks
func (srv *Server) blockexplorerBlockDataHandler(w http.ResponseWriter, req *http.Request) {
	// Extract the start and end point from the request
	var start, finish types.BlockHeight
	_, err := fmt.Sscan(req.FormValue("start"), &start)
	if err != nil {
		writeError(w, "Malformed or no start height", http.StatusBadRequest)
		return
	}

	// If a range end is not given, assume the range end to be one
	// greater than the range start, returning one block
	_, err = fmt.Sscan(req.FormValue("finish"), &finish)
	if err != nil {
		finish = start + 1
	}

	// Bounds checking is done inside BlockInfo
	blockSummaries, err := srv.blocke.BlockInfo(start, finish)
	if err != nil {
		writeError(w, err.Error(), http.StatusBadRequest)
		return
	}

	writeJSON(w, blockSummaries)
}
开发者ID:mm3,项目名称:Sia,代码行数:26,代码来源:blockexplorer.go


示例15: main

func main() {
	sq := make(map[int]bool)
	for i := 0; i < 46341; i++ {
		sq[i*i] = true
	}

	data, err := os.Open(os.Args[1])
	if err != nil {
		log.Fatal(err)
	}
	defer data.Close()
	scanner := bufio.NewScanner(data)
	scanner.Scan()
	var n int
	fmt.Sscan(scanner.Text(), &n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		var x int
		fmt.Sscan(scanner.Text(), &x)
		num, l, bot, top := 0, make(map[int]bool), sqrt(x/2), sqrt(x)+1
		for j := bot; j < top; j++ {
			t := x - j*j
			if sq[t] && !l[t] {
				l[j*j], num = true, num+1
			}
		}
		fmt.Println(num)
	}
}
开发者ID:oorja,项目名称:ce-challenges,代码行数:29,代码来源:double_squares.go


示例16: GetEnvs

// Convert and validate the GOAGAIN_FD and GOAGAIN_PPID environment
// variables.  If both are present and in order, this is a child process
// that may pick up where the parent left off.
func GetEnvs() (*net.TCPListener, int, error) {
	envFd := os.Getenv("GOAGAIN_FD")
	if "" == envFd {
		return nil, 0, errors.New("GOAGAIN_FD not set")
	}
	var fd uintptr
	_, err := fmt.Sscan(envFd, &fd)
	if nil != err {
		return nil, 0, err
	}
	tmp, err := net.FileListener(os.NewFile(fd, "listener"))
	if nil != err {
		return nil, 0, err
	}
	l := tmp.(*net.TCPListener)
	envPpid := os.Getenv("GOAGAIN_PPID")
	if "" == envPpid {
		return l, 0, errors.New("GOAGAIN_PPID not set")
	}
	var ppid int
	_, err = fmt.Sscan(envPpid, &ppid)
	if nil != err {
		return l, 0, err
	}
	if syscall.Getppid() != ppid {
		return l, ppid, errors.New(fmt.Sprintf(
			"GOAGAIN_PPID is %d but parent is %d\n", ppid, syscall.Getppid()))
	}
	return l, ppid, nil
}
开发者ID:braintreeps,项目名称:goagain,代码行数:33,代码来源:goagain.go


示例17: TestDecScan

func TestDecScan(t *testing.T) {
	tmp := new(inf.Dec)
	for i, test := range decStringTests {
		if test.scale < 0 {
			// SetString only supports scale >= 0
			continue
		}
		// initialize to a non-zero value so that issues with parsing
		// 0 are detected
		tmp.Set(inf.NewDec(1234567890, 123))
		n1, n2 := new(inf.Dec), tmp
		nn1, err1 := fmt.Sscan(test.in, n1)
		nn2, err2 := fmt.Sscan(test.in, n2)
		if !test.scanOk {
			if err1 == nil || err2 == nil {
				t.Errorf("#%d (input '%s') ok incorrect, should be %t", i, test.in, test.scanOk)
			}
			continue
		}
		expected := inf.NewDec(test.val, test.scale)
		if nn1 != 1 || err1 != nil || nn2 != 1 || err2 != nil {
			t.Errorf("#%d (input '%s') error %d %v, %d %v", i, test.in, nn1, err1, nn2, err2)
			continue
		}
		if n1.Cmp(expected) != 0 {
			t.Errorf("#%d (input '%s') got: %s want: %d", i, test.in, n1, test.val)
		}
		if n2.Cmp(expected) != 0 {
			t.Errorf("#%d (input '%s') got: %s want: %d", i, test.in, n2, test.val)
		}
	}
}
开发者ID:dgonyeo,项目名称:acpush-orig,代码行数:32,代码来源:dec_test.go


示例18: readRow

func (t *Table) readRow(rowNum int, rowValues []*interface{}) error {
	offset := t.RowOffsets[rowNum]
	for i, v := range t.ColumnTypes {
		var size int16
		var rep string
		binary.Read(cursor{&t.Data, offset}, binary.LittleEndian, &size)
		offset = offset + 2
		if size == -1 {
			*(rowValues[i]) = nil
			continue
		} else {
			rep = string(t.Data[offset : offset+int(size)])
			offset = offset + int(size)
		}
		switch v {
		case IntegerDatatype:
			var value int64
			_, err := fmt.Sscan(rep, &value)
			if err != nil {
				return err
			}
			*(rowValues[i]) = value
		case FloatDatatype:
			var value float64
			_, err := fmt.Sscan(rep, &value)
			if err != nil {
				return err
			}
			*(rowValues[i]) = value
		case StringDatatype:
			*(rowValues[i]) = rep
		}
	}
	return nil
}
开发者ID:timob,项目名称:gemini,代码行数:35,代码来源:table.go


示例19: memoryItem

func memoryItem(font *Font, offset int, image *xgraphics.Image, window *xwindow.Window) *Item {
	channel := make(chan string, 8)
	go func() {
		file, err := os.Open("/proc/meminfo")
		if err != nil {
			log.Fatal(err)
		}

		for time := range time.NewTicker(time.Second).C {
			_ = time

			text, err := ioutil.ReadAll(file)
			if err != nil {
				log.Fatal(err)
			}
			_, err = file.Seek(0, 0)
			if err != nil {
				log.Fatal(err)
			}

			fields := strings.Fields(string(text))
			var mem_total, mem_free uint64
			fmt.Sscan(fields[1], &mem_total)
			fmt.Sscan(fields[4], &mem_free)

			channel <- fmt.Sprintf("Memory: %3v%%", (100*(mem_total-mem_free))/mem_total)
		}
	}()
	return &Item{Text: channel, Font: font, offset: offset, image: image, window: window}
}
开发者ID:pointlander,项目名称:csm,代码行数:30,代码来源:csm.go


示例20: parseMoveOp

// Parse human-readable coordinates into a move operation
func parseMoveOp(from, to string) (op bopMovePiece, err error) {
	_, err = fmt.Sscan(from, &op.from)
	if err != nil {
		return
	}
	_, err = fmt.Sscan(to, &op.to)
	return
}
开发者ID:hraban,项目名称:chess,代码行数:9,代码来源:chess.go



注:本文中的fmt.Sscan函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang fmt.Sscanf函数代码示例发布时间:2022-05-24
下一篇:
Golang fmt.Sprintln函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap