> ## Content Index
> Fetch the complete content index at: https://blog.hacking.pub/llms.txt
> Use this file to discover other available public pages before exploring further.

# go NaN
- URL: https://blog.hacking.pub/2019/07/21/go-nan/
- Published: 2019-07-21T13:39:09.000Z
- Updated: 2019-07-21T13:39:09.000Z
- Author: chainhelen
- Tags: go, go语言, golang, 源码剖析, go源码, NaN

###### NaN

之前阅读源码，发现`golang`的可比性和`map`里面都有`NaN`的特判  
以前写`js`可知，`NaN`与任何浮点型比较都是`false`，包括它自己

```
// main.go
package main

import (
	"fmt"
	"math"
)

type TestFloat struct {
	Float64 float64 `json:"float64"`
}

func main() {
	mp := make(map[float64]int)
	mp[math.NaN()] = 12
	mp[math.NaN()] = 14

	for i, v := range mp {
		fmt.Printf("%f %d\n", i, v)
	}
}

// ./main
NaN 12
NaN 14

```

###### 获取

经过测试，类似`strconv`、`encoding/json`正常解析一个非正常`float64`字符串到`float64`，都是直接报错的，并没有出现`NaN`，目前我只发现底下这一种可能会遇到的业务场景

```
var i flaot64 = 0.0
fmt.Printf("%s\n", i)

```