package main import "fmt" type Vertex struct { Lat, Long float64 } var m map[string]Vertex func main() { m = make(map[string]Vertex) m["Bell Labs"] = Vertex{ 40.68433, -74.39967, } fmt.Println(m["Bell Labs"]) }
map创建必须是make得,而不是new出来
http://blog.sina.com.cn/s/blog_487109d101018fcx.html
go中的闭包
闭包函数出现的条件:
1.被嵌套的函数引用到非本函数的外部变量,而且这外部变量不是“全局变量”
2.嵌套的函数被独立了出来(被父函数返回或赋值 变成了独立的个体),
而被引用的变量所在的父函数已结束
还找到句明言:对象是附有行为的数据,而闭包是附有数据的行为。
近来看了“无闻”的视频教程后对“闭包”有了新的理解
(对外引用变量其实是 获取了该变量的指针)
package main
import (
"fmt"
)
func main (){
fmt.Println("hell ")
ex := Exfunc(12)
ex()
ex2 := Exfunc(123)
ex2()
ex3 := Exfunc2(123)
ex3()
}
func Exfunc (n int) func (){
sum:=n
a := func () {
fmt.Println(sum+1)
}
return a
}
func Exfunc2 (n int) func (){
return func () {
fmt.Println(n+1)
}
}
http://blog.csdn.net/lmj623565791/article/details/42628537
listfragment的startforresult:
public class ListTitleFragment extends ListFragment { public static final int REQUEST_DETAIL = 0x110; private List<String> mTitles = Arrays.asList("Hello", "World", "Android"); private int mCurrentPos ; private ArrayAdapter<String> mAdapter ; @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setListAdapter(mAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, mTitles)); } @Override public void onListItemClick(ListView l, View v, int position, long id) { mCurrentPos = position ; Intent intent = new Intent(getActivity(),ContentActivity.class); intent.putExtra(ContentFragment.ARGUMENT, mTitles.get(position)); startActivityForResult(intent, REQUEST_DETAIL); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { Log.e("TAG", "onActivityResult"); super.onActivityResult(requestCode, resultCode, data); if(requestCode == REQUEST_DETAIL) { mTitles.set(mCurrentPos, mTitles.get(mCurrentPos)+" -- "+data.getStringExtra(ContentFragment.RESPONSE)); mAdapter.notifyDataSetChanged(); } } }
下面描述一个简单的场景,比如我们某个按钮触发Activity跳转,需要通过Intent传递参数到目标Activity的Fragment中,那么此Fragment如何获取当前的Intent的值呢?
有哥们会说,这个简单?看我的代码(问题代码):